回答編集履歴
3
修正
test
CHANGED
@@ -108,7 +108,7 @@
|
|
108
108
|
|
109
109
|
self.labels = []
|
110
110
|
|
111
|
-
name_to_label = {"
|
111
|
+
name_to_label = {"cat": 0, "dog": 1}
|
112
112
|
|
113
113
|
|
114
114
|
|
2
修正
test
CHANGED
@@ -73,3 +73,103 @@
|
|
73
73
|
参考リンク ↓ ImageFolder を使った学習の例
|
74
74
|
|
75
75
|
[Pytorch - 事前学習モデルを使ってクラス分類モデルを学習する方法 - pystyle](https://pystyle.info/pytorch-train-classification-problem-using-a-pretrained-model/)
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
## 追記
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
```python
|
84
|
+
|
85
|
+
import glob
|
86
|
+
|
87
|
+
import os
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
from PIL import Image
|
92
|
+
|
93
|
+
from torch.utils import data as data
|
94
|
+
|
95
|
+
from torchvision import transforms as transforms
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
class MyDatasets(data.Dataset):
|
102
|
+
|
103
|
+
def __init__(self, root_dir, key, transform):
|
104
|
+
|
105
|
+
self.transform = transform
|
106
|
+
|
107
|
+
self.data = []
|
108
|
+
|
109
|
+
self.labels = []
|
110
|
+
|
111
|
+
name_to_label = {"dog": 0, "cat": 1}
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
target_path_list = []
|
116
|
+
|
117
|
+
target_dir = os.path.join(root_dir, key, "**/*")
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
for path in glob.glob(target_dir):
|
122
|
+
|
123
|
+
name = os.path.basename(os.path.dirname(path))
|
124
|
+
|
125
|
+
label = name_to_label[name]
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
self.data.append(path)
|
130
|
+
|
131
|
+
self.labels.append(label)
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
def __len__(self):
|
136
|
+
|
137
|
+
return len(self.data)
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
def __getitem__(self, index):
|
142
|
+
|
143
|
+
img_path = self.data[index]
|
144
|
+
|
145
|
+
label = self.labels[index]
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
img = Image.open(img_path).convert("RGB")
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
img = self.transform(img)
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
return img, label
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
transform = transforms.Compose([transforms.Resize((256, 256)), transforms.ToTensor()])
|
164
|
+
|
165
|
+
train_dataset = MyDatasets("/data/mydataset", "train", transform)
|
166
|
+
|
167
|
+
train_dataloader = data.DataLoader(train_dataset, batch_size=32)
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
for data, labels in train_dataloader:
|
172
|
+
|
173
|
+
print(data.shape, labels.shape)
|
174
|
+
|
175
|
+
```
|
1
修正
test
CHANGED
@@ -1,3 +1,31 @@
|
|
1
|
+
## 質問のコードでエラーが起こっている原因
|
2
|
+
|
3
|
+
|
4
|
+
|
5
|
+
`train_dataset = MyDatasets()` で引数を指定していないので、`self.path` が None になっています。
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
それを以下で文字列と結合しようとして、None と str の結合はできないとエラーになっています。
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
```python
|
14
|
+
|
15
|
+
target_path = os.path.join(self.path + self.key + '/**/*.jpg')
|
16
|
+
|
17
|
+
```
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
## 提案
|
26
|
+
|
27
|
+
|
28
|
+
|
1
29
|
```
|
2
30
|
|
3
31
|
animal_dataset
|