質問編集履歴

1

コードが足りなかったため

2020/10/29 13:05

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -22,6 +22,24 @@
22
22
 
23
23
  ```
24
24
 
25
+ Traceback (most recent call last):
26
+
27
+ File "learning2.py", line 161, in <module>
28
+
29
+ shuffle=True,callbacks=[early_stopping])
30
+
31
+ File "/home/mlab-user/anaconda3/envs/image/lib/python3.5/site-packages/keras/engine/training.py", line 970, in fit
32
+
33
+ batch_size=batch_size)
34
+
35
+ File "/home/mlab-user/anaconda3/envs/image/lib/python3.5/site-packages/keras/engine/training.py", line 787, in _standardize_user_data
36
+
37
+ exception_prefix='target')
38
+
39
+ File "/home/mlab-user/anaconda3/envs/image/lib/python3.5/site-packages/keras/engine/training_utils.py", line 127, in standardize_input_data
40
+
41
+ 'with shape ' + str(data_shape))
42
+
25
43
  ValueError: Error when checking target: expected dense_2 to have 2 dimensions, but got array with shape (10800, 3, 3, 3)
26
44
 
27
45
 
@@ -36,7 +54,81 @@
36
54
 
37
55
  ```ここに言語名を入力
38
56
 
57
+ import keras
58
+
59
+ from keras.utils import np_utils
60
+
61
+ from keras.models import Sequential
62
+
63
+ from keras.layers.convolutional import MaxPooling2D
64
+
65
+ from keras.layers import Conv2D, Flatten, Dense, Dropout
66
+
67
+ from sklearn.model_selection import train_test_split
68
+
69
+ from PIL import Image
70
+
71
+ from keras.callbacks import EarlyStopping
72
+
73
+ import numpy as np
74
+
75
+ import glob
76
+
77
+ import matplotlib.pyplot as plt
78
+
79
+ from sklearn.model_selection import KFold
80
+
81
+ from sklearn.model_selection import train_test_split
82
+
83
+ from sklearn.metrics import confusion_matrix
84
+
85
+
86
+
87
+
88
+
89
+ classes = ["犬","猫","兎"
90
+
91
+ ]
92
+
93
+
94
+
39
- # num_classes=3
95
+ num_classes = len(classes)
96
+
97
+
98
+
99
+ X = []
100
+
101
+ Y = []
102
+
103
+ for index, classlabel in enumerate(classes):
104
+
105
+ dir = "./" + classlabel
106
+
107
+ files = glob.glob(dir + "/*.jpg")
108
+
109
+ for i, file in enumerate(files):
110
+
111
+ image = Image.open(file)
112
+
113
+ image = image.convert("RGB")
114
+
115
+ image = image.resize((128, 128))
116
+
117
+ data = np.asarray(image)
118
+
119
+ X.append(data)
120
+
121
+ Y.append(index)
122
+
123
+
124
+
125
+ X = np.array(X)
126
+
127
+ Y = np.array(Y)
128
+
129
+ X = X.astype('float32')/255.0
130
+
131
+
40
132
 
41
133
  Y = np_utils.to_categorical(Y, num_classes)
42
134
 
@@ -98,6 +190,16 @@
98
190
 
99
191
 
100
192
 
193
+ model.add(Conv2D(128, (3, 3), padding='same', activation="relu"))
194
+
195
+ model.add(Conv2D(128, (3, 3), padding='same', activation="relu"))
196
+
197
+ model.add(MaxPooling2D(pool_size=(2, 2)))
198
+
199
+ model.add(Dropout(0.25))
200
+
201
+
202
+
101
203
  model.add(Flatten())
102
204
 
103
205
  model.add(Dense(128 ,activation="relu"))
@@ -124,7 +226,7 @@
124
226
 
125
227
 
126
228
 
127
- hist = model.fit(train_data, train_label, batch_size=16384, verbose=1,
229
+ hist = model.fit(train_data, train_label, batch_size=10, verbose=1,
128
230
 
129
231
  epochs=10, validation_data=(val_data, y_test),
130
232
 
@@ -142,6 +244,8 @@
142
244
 
143
245
  model.save('./cnn.h5')
144
246
 
247
+
248
+
145
249
  ```
146
250
 
147
251