質問編集履歴

1

エラーを省略してしまっていた。

2020/05/15 11:23

投稿

Hiro051
Hiro051

スコア9

test CHANGED
File without changes
test CHANGED
@@ -10,220 +10,228 @@
10
10
 
11
11
  ### 発生している問題・エラーメッセージ
12
12
 
13
+ Traceback (most recent call last):
14
+
15
+ File "Arashi/arashi.py", line 52, in <module>
16
+
17
+ datasets = torch.utils.data.TensorDataset(img_datas, labels)
18
+
19
+ File "/home/selen/.pyenv/versions/3.7.3/lib/python3.7/site-packages/torch/utils/data/dataset.py", line 36, in __init__
20
+
13
21
  assert all(tensors[0].size(0) == tensor.size(0) for tensor in tensors)
14
22
 
23
+ File "/home/selen/.pyenv/versions/3.7.3/lib/python3.7/site-packages/torch/utils/data/dataset.py", line 36, in <genexpr>
24
+
25
+ assert all(tensors[0].size(0) == tensor.size(0) for tensor in tensors)
26
+
15
27
  AttributeError: 'list' object has no attribute 'size'
16
28
 
29
+ ### 該当のソースコード
30
+
31
+ import torch, torchvision
32
+
33
+ import torch.nn as nn
34
+
35
+ import torch.nn.functional as F
36
+
37
+ from torchvision import transforms
38
+
39
+ import pytorch_lightning as pl
40
+
41
+ from pytorch_lightning import Trainer
42
+
43
+
44
+
45
+ from PIL import Image
46
+
47
+ import glob
48
+
49
+
50
+
51
+ fold_path = '/home/selen/downloads/'
52
+
53
+ imgs = []
54
+
55
+ for imgs_path in glob.glob(fold_path + '*'):
56
+
57
+ imgs.append(glob.glob(imgs_path + '/*'))
58
+
59
+
60
+
61
+ from torchvision.models import resnet18
62
+
63
+ resnet = resnet18(pretrained=True)
64
+
65
+
66
+
67
+ transform = transforms.Compose([
68
+
69
+ transforms.Resize((224, 224)),
70
+
71
+ transforms.ToTensor(),
72
+
73
+ transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
74
+
75
+ ])
76
+
77
+
78
+
79
+ labels = []
80
+
81
+ img_datas = torch.tensor([])
82
+
83
+
84
+
85
+ for i,imgs_arr in enumerate(imgs):
86
+
87
+
88
+
89
+ for img_path in imgs_arr:
90
+
91
+ labels.append(i)
92
+
93
+ img = Image.open(img_path)
94
+
95
+ tensor_img = transform(img)
96
+
97
+ tensor_img = tensor_img.unsqueeze(0)
98
+
99
+ img_datas = torch.cat([img_datas, tensor_img],dim=0)
100
+
101
+
102
+
103
+ datasets = torch.utils.data.TensorDataset(img_datas, labels)
104
+
105
+
106
+
107
+
108
+
109
+ n_train = int(len(datasets) * 0.85)
110
+
111
+ n_val = len(datasets) - n_train
112
+
113
+ torch.manual_seed(0)
114
+
115
+ train,val = torch.utils.data.random_split(datasets,[n_train,n_val])
116
+
117
+
118
+
119
+ class TrainNet(pl.LightningModule):
120
+
121
+ @pl.data_loader
122
+
123
+ def train_dataloader(self):
124
+
125
+ return torch.utils.data.DataLoader(train, self.batch_size,shuffle=True)
126
+
127
+
128
+
129
+ def training_step(self, batch, batch_nb):
130
+
131
+ x, t = batch
132
+
133
+ y = self.forward(x)
134
+
135
+ loss = self.lossfun(y, t)
136
+
137
+ results = {'loss': loss}
138
+
139
+ return results
140
+
141
+
142
+
143
+ class ValidationNet(pl.LightningModule):
144
+
145
+
146
+
147
+ @pl.data_loader
148
+
149
+ def val_dataloader(self):
150
+
151
+ return torch.utils.data.DataLoader(val, self.batch_size)
152
+
153
+
154
+
155
+ def validation_step(self, batch, batch_nb):
156
+
157
+ x, t = batch
158
+
159
+ y = self.forward(x)
160
+
161
+ loss = self.lossfun(y, t)
162
+
163
+ y_label = torch.argmax(y, dim=1)
164
+
165
+ acc = torch.sum(t == y_label) * 1.0 / len(t)
166
+
167
+ results = {'val_loss': loss, 'val_acc': acc}
168
+
169
+ return results
170
+
171
+
172
+
173
+ def validation_end(self, outputs):
174
+
175
+ avg_loss = torch.stack([x['val_loss'] for x in outputs]).mean()
176
+
177
+ avg_acc = torch.stack([x['val_acc'] for x in outputs]).mean()
178
+
179
+ results = {'val_loss': avg_loss, 'val_acc': avg_acc}
180
+
181
+ return results
182
+
183
+
184
+
185
+ class FineTuningNet(TrainNet, ValidationNet):
186
+
187
+
188
+
189
+ def __init__(self, batch_size=256):
190
+
191
+ super().__init__()
192
+
193
+ self.batch_size = batch_size
194
+
195
+ self.conv = resnet18(pretrained=True)
196
+
197
+ self.fc1 = nn.Linear(1000, 100)
198
+
199
+ self.fc2 = nn.Linear(100, 5)
200
+
201
+ for param in self.conv.parameters():
202
+
203
+ param.requires_grad = False
204
+
205
+
206
+
207
+ def lossfun(self, y, t):
208
+
209
+ return F.cross_entropy(y, t)
210
+
211
+
212
+
213
+ def configure_optimizers(self):
214
+
215
+ return torch.optim.SGD(self.parameters(), lr=0.01)
216
+
217
+
218
+
219
+ def forward(self, x):
220
+
221
+ x = self.conv(x)
222
+
223
+ x = self.fc1(x)
224
+
225
+ x = F.relu(x)
226
+
227
+ x = self.fc2(x)
228
+
229
+ return x
230
+
17
231
  ```
18
232
 
19
233
 
20
234
 
21
- ### 該当のソースコード
22
-
23
- import torch, torchvision
24
-
25
- import torch.nn as nn
26
-
27
- import torch.nn.functional as F
28
-
29
- from torchvision import transforms
30
-
31
- import pytorch_lightning as pl
32
-
33
- from pytorch_lightning import Trainer
34
-
35
-
36
-
37
- from PIL import Image
38
-
39
- import glob
40
-
41
-
42
-
43
- fold_path = '/home/selen/downloads/'
44
-
45
- imgs = []
46
-
47
- for imgs_path in glob.glob(fold_path + '*'):
48
-
49
- imgs.append(glob.glob(imgs_path + '/*'))
50
-
51
-
52
-
53
- from torchvision.models import resnet18
54
-
55
- resnet = resnet18(pretrained=True)
56
-
57
-
58
-
59
- transform = transforms.Compose([
60
-
61
- transforms.Resize((224, 224)),
62
-
63
- transforms.ToTensor(),
64
-
65
- transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
66
-
67
- ])
68
-
69
-
70
-
71
- labels = []
72
-
73
- img_datas = torch.tensor([])
74
-
75
-
76
-
77
- for i,imgs_arr in enumerate(imgs):
78
-
79
-
80
-
81
- for img_path in imgs_arr:
82
-
83
- labels.append(i)
84
-
85
- img = Image.open(img_path)
86
-
87
- tensor_img = transform(img)
88
-
89
- tensor_img = tensor_img.unsqueeze(0)
90
-
91
- img_datas = torch.cat([img_datas, tensor_img],dim=0)
92
-
93
-
94
-
95
- datasets = torch.utils.data.TensorDataset(img_datas, labels)
96
-
97
-
98
-
99
-
100
-
101
- n_train = int(len(datasets) * 0.85)
102
-
103
- n_val = len(datasets) - n_train
104
-
105
- torch.manual_seed(0)
106
-
107
- train,val = torch.utils.data.random_split(datasets,[n_train,n_val])
108
-
109
-
110
-
111
- class TrainNet(pl.LightningModule):
112
-
113
- @pl.data_loader
114
-
115
- def train_dataloader(self):
116
-
117
- return torch.utils.data.DataLoader(train, self.batch_size,shuffle=True)
118
-
119
-
120
-
121
- def training_step(self, batch, batch_nb):
122
-
123
- x, t = batch
124
-
125
- y = self.forward(x)
126
-
127
- loss = self.lossfun(y, t)
128
-
129
- results = {'loss': loss}
130
-
131
- return results
132
-
133
-
134
-
135
- class ValidationNet(pl.LightningModule):
136
-
137
-
138
-
139
- @pl.data_loader
140
-
141
- def val_dataloader(self):
142
-
143
- return torch.utils.data.DataLoader(val, self.batch_size)
144
-
145
-
146
-
147
- def validation_step(self, batch, batch_nb):
148
-
149
- x, t = batch
150
-
151
- y = self.forward(x)
152
-
153
- loss = self.lossfun(y, t)
154
-
155
- y_label = torch.argmax(y, dim=1)
156
-
157
- acc = torch.sum(t == y_label) * 1.0 / len(t)
158
-
159
- results = {'val_loss': loss, 'val_acc': acc}
160
-
161
- return results
162
-
163
-
164
-
165
- def validation_end(self, outputs):
166
-
167
- avg_loss = torch.stack([x['val_loss'] for x in outputs]).mean()
168
-
169
- avg_acc = torch.stack([x['val_acc'] for x in outputs]).mean()
170
-
171
- results = {'val_loss': avg_loss, 'val_acc': avg_acc}
172
-
173
- return results
174
-
175
-
176
-
177
- class FineTuningNet(TrainNet, ValidationNet):
178
-
179
-
180
-
181
- def __init__(self, batch_size=256):
182
-
183
- super().__init__()
184
-
185
- self.batch_size = batch_size
186
-
187
- self.conv = resnet18(pretrained=True)
188
-
189
- self.fc1 = nn.Linear(1000, 100)
190
-
191
- self.fc2 = nn.Linear(100, 5)
192
-
193
- for param in self.conv.parameters():
194
-
195
- param.requires_grad = False
196
-
197
-
198
-
199
- def lossfun(self, y, t):
200
-
201
- return F.cross_entropy(y, t)
202
-
203
-
204
-
205
- def configure_optimizers(self):
206
-
207
- return torch.optim.SGD(self.parameters(), lr=0.01)
208
-
209
-
210
-
211
- def forward(self, x):
212
-
213
- x = self.conv(x)
214
-
215
- x = self.fc1(x)
216
-
217
- x = F.relu(x)
218
-
219
- x = self.fc2(x)
220
-
221
- return x
222
-
223
- ```
224
-
225
-
226
-
227
235
 
228
236
 
229
237
  ### 試したこと