質問編集履歴
3
文章表現
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
画像データを
|
1
|
+
画像データの特徴量を抽出し機械学習用のデータセットを作りたい
|
test
CHANGED
@@ -4,9 +4,9 @@
|
|
4
4
|
|
5
5
|
自前の画像データから機械学習用のデータセットを作成し学習させたいのですが、
|
6
6
|
|
7
|
-
下記エラーにより実行できません。画像データを配列にする
|
7
|
+
モデルをfitさせた時に下記エラーにより実行できません。画像データを配列にする
|
8
|
-
|
8
|
+
|
9
|
-
と考えているのですが、解決方法をご存知の方ご教示ください。
|
9
|
+
ときの方法が不適切ではと考えているのですが、解決方法をご存知の方ご教示ください。
|
10
10
|
|
11
11
|
|
12
12
|
|
@@ -52,8 +52,6 @@
|
|
52
52
|
|
53
53
|
|
54
54
|
|
55
|
-
#1.anotation_results_form_check.ipynbで作成したdata2.csvを読み込み
|
56
|
-
|
57
55
|
df2 = pd.read_csv("data2.csv")
|
58
56
|
|
59
57
|
df2.shape
|
@@ -108,7 +106,7 @@
|
|
108
106
|
|
109
107
|
|
110
108
|
|
111
|
-
#targetにsmileのカテゴリを代入
|
109
|
+
**#targetにsmileのカテゴリを代入
|
112
110
|
|
113
111
|
smiles =df_join["smile"]
|
114
112
|
|
@@ -122,7 +120,7 @@
|
|
122
120
|
|
123
121
|
target.append(data)
|
124
122
|
|
125
|
-
y = np.array(target)
|
123
|
+
y = np.array(target)**
|
126
124
|
|
127
125
|
y
|
128
126
|
|
@@ -162,6 +160,26 @@
|
|
162
160
|
|
163
161
|
x = np.array(photo_array)
|
164
162
|
|
163
|
+
x[0]
|
164
|
+
|
165
|
+
```
|
166
|
+
|
167
|
+
```OUT
|
168
|
+
|
169
|
+
array([[217, 217, 217, ..., 188, 196, 203],
|
170
|
+
|
171
|
+
[217, 217, 217, ..., 188, 196, 203],
|
172
|
+
|
173
|
+
[217, 217, 217, ..., 188, 196, 204],
|
174
|
+
|
175
|
+
...,
|
176
|
+
|
177
|
+
[ 4, 4, 4, ..., 44, 43, 42],
|
178
|
+
|
179
|
+
[ 4, 4, 4, ..., 44, 43, 42],
|
180
|
+
|
181
|
+
[ 4, 4, 4, ..., 44, 43, 41]], dtype=uint8)
|
182
|
+
|
165
183
|
```
|
166
184
|
|
167
185
|
```
|
2
文法の修正
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
画像データ
|
1
|
+
画像データをnumpy配列にし機械学習用のデータセットを作りたい
|
test
CHANGED
File without changes
|
1
不具合内容を変更
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
画像データから機械学習用のデータセットを作る時にエラーが発生(setting an array element with a sequence.)
|
test
CHANGED
@@ -20,206 +20,308 @@
|
|
20
20
|
|
21
21
|
TypeError Traceback (most recent call last)
|
22
22
|
|
23
|
+
TypeError: only size-1 arrays can be converted to Python scalars
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
The above exception was the direct cause of the following exception:
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
ValueError Traceback (most recent call last)
|
32
|
+
|
33
|
+
ValueError: setting an array element with a sequence.
|
34
|
+
|
35
|
+
```
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
### 該当のソースコード
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
```python
|
44
|
+
|
45
|
+
import pandas as pd
|
46
|
+
|
47
|
+
import numpy as np
|
48
|
+
|
49
|
+
import glob
|
50
|
+
|
51
|
+
import cv2
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
#1.anotation_results_form_check.ipynbで作成したdata2.csvを読み込み
|
56
|
+
|
57
|
+
df2 = pd.read_csv("data2.csv")
|
58
|
+
|
59
|
+
df2.shape
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
#file pathの設定(フォルダ内の画像データを読み込み 画像データは6486枚)
|
66
|
+
|
67
|
+
files = glob.glob("/Users/a440/Desktop/happy_images/*")
|
68
|
+
|
69
|
+
len(files)
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
#filesのデータフレームを作成
|
74
|
+
|
75
|
+
df_files = pd.DataFrame({"data":files})
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
#dataのformatを合わせる
|
80
|
+
|
81
|
+
df2["data"] = "/Users/a440/Desktop/happy_images/" + df2["data"] + ".jpg"
|
82
|
+
|
83
|
+
df2 = df2.drop("Unnamed: 0", axis=1)
|
84
|
+
|
85
|
+
df2.to_csv("data2-2.csv")
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
#df2とdf_filesをdata列でマージする
|
90
|
+
|
91
|
+
df_join = pd.merge(df2, df_files, how="inner",on="data",indicator=True)
|
92
|
+
|
93
|
+
df_join.shape
|
94
|
+
|
95
|
+
```
|
96
|
+
|
97
|
+
```OUT
|
98
|
+
|
99
|
+
6441, 3
|
100
|
+
|
101
|
+
```
|
102
|
+
|
103
|
+
```
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
df_join.to_csv("df_join.csv")
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
#targetにsmileのカテゴリを代入
|
112
|
+
|
113
|
+
smiles =df_join["smile"]
|
114
|
+
|
115
|
+
target = []
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
for smile in smiles:
|
120
|
+
|
121
|
+
data = np.asarray(smile)
|
122
|
+
|
123
|
+
target.append(data)
|
124
|
+
|
125
|
+
y = np.array(target)
|
126
|
+
|
127
|
+
y
|
128
|
+
|
129
|
+
```
|
130
|
+
|
131
|
+
```OUT
|
132
|
+
|
133
|
+
array([1, 3, 1, ..., 3, 3, 3])
|
134
|
+
|
135
|
+
```
|
136
|
+
|
137
|
+
```
|
138
|
+
|
139
|
+
#photo_array に画像データの配列を代入
|
140
|
+
|
141
|
+
titles = df_join["data"]
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
image_size = 50
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
photo_array = []
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
for title in titles:
|
156
|
+
|
157
|
+
bgr = cv2.imread(title, cv2.IMREAD_GRAYSCALE)
|
158
|
+
|
159
|
+
bgr = np.asarray(bgr)
|
160
|
+
|
161
|
+
photo_array.append(bgr)
|
162
|
+
|
163
|
+
x = np.array(photo_array)
|
164
|
+
|
165
|
+
```
|
166
|
+
|
167
|
+
```
|
168
|
+
|
169
|
+
from sklearn import linear_model
|
170
|
+
|
171
|
+
clf = linear_model.LogisticRegression()
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
from sklearn.model_selection import StratifiedKFold
|
176
|
+
|
177
|
+
ss = StratifiedKFold(n_splits=10,
|
178
|
+
|
179
|
+
shuffle=True)
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
for train_index, test_index in ss.split(x, y):
|
184
|
+
|
185
|
+
x_train, x_test = x[train_index], x[test_index]
|
186
|
+
|
187
|
+
y_train, y_test = y[train_index], y[test_index]
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
clf.fit(x_train, y_train)
|
192
|
+
|
193
|
+
print(clf.score(x_test, y_test))
|
194
|
+
|
195
|
+
```
|
196
|
+
|
197
|
+
```エラーメッセージ
|
198
|
+
|
199
|
+
---------------------------------------------------------------------------
|
200
|
+
|
201
|
+
TypeError Traceback (most recent call last)
|
202
|
+
|
203
|
+
TypeError: only size-1 arrays can be converted to Python scalars
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
The above exception was the direct cause of the following exception:
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
ValueError Traceback (most recent call last)
|
212
|
+
|
23
|
-
<ipython-input-1
|
213
|
+
<ipython-input-15-305a5bf690e8> in <module>
|
24
|
-
|
25
|
-
|
214
|
+
|
26
|
-
|
27
|
-
8 for train_index, test_index in ss.split(photo_array, target):
|
28
|
-
|
29
|
-
----> 9 x_train, x_test = photo_array[train_index], photo_array[test_index]
|
30
|
-
|
31
|
-
10 y_train, y_test =
|
215
|
+
10 y_train, y_test = y[train_index], y[test_index]
|
32
216
|
|
33
217
|
11
|
34
218
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
i
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
i
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
d
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
d
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
targ
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
3
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
6439 3
|
146
|
-
|
147
|
-
6440 3
|
148
|
-
|
149
|
-
Name: smile, Length: 6441, dtype: int64
|
150
|
-
|
151
|
-
```
|
152
|
-
|
153
|
-
```
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
titles = df_join["data"]
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
photo_array = []
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
for title in titles:
|
166
|
-
|
167
|
-
bgr = cv2.imread(title, cv2.IMREAD_GRAYSCALE)
|
168
|
-
|
169
|
-
bgr = np.array(bgr)
|
170
|
-
|
171
|
-
photo_array.append(bgr)
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
photo_array[0]
|
176
|
-
|
177
|
-
```
|
178
|
-
|
179
|
-
```OUT
|
180
|
-
|
181
|
-
array([[217, 217, 217, ..., 188, 196, 203],
|
182
|
-
|
183
|
-
[217, 217, 217, ..., 188, 196, 203],
|
184
|
-
|
185
|
-
[217, 217, 217, ..., 188, 196, 204],
|
186
|
-
|
187
|
-
...,
|
188
|
-
|
189
|
-
[ 4, 4, 4, ..., 44, 43, 42],
|
190
|
-
|
191
|
-
[ 4, 4, 4, ..., 44, 43, 42],
|
192
|
-
|
193
|
-
[ 4, 4, 4, ..., 44, 43, 41]], dtype=uint8)
|
194
|
-
|
195
|
-
```
|
196
|
-
|
197
|
-
```
|
198
|
-
|
199
|
-
from sklearn import linear_model
|
200
|
-
|
201
|
-
clf = linear_model.LogisticRegression()
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
from sklearn.model_selection import StratifiedKFold
|
206
|
-
|
207
|
-
ss = StratifiedKFold(n_splits=10,
|
208
|
-
|
209
|
-
shuffle=True)
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
for train_index, test_index in ss.split(photo_array, target):
|
214
|
-
|
215
|
-
x_train, x_test = photo_array[train_index], photo_array[test_index]
|
216
|
-
|
217
|
-
y_train, y_test = target[train_index], target[test_index]
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
clf.fit(x_train, y_train)
|
222
|
-
|
223
|
-
print(clf.score(x_test, y_test))
|
224
|
-
|
225
|
-
```
|
219
|
+
---> 12 clf.fit(x_train, y_train)
|
220
|
+
|
221
|
+
13 print(clf.score(x_test, y_test))
|
222
|
+
|
223
|
+
14
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
/opt/anaconda3/lib/python3.7/site-packages/sklearn/linear_model/_logistic.py in fit(self, X, y, sample_weight)
|
228
|
+
|
229
|
+
1342 X, y = self._validate_data(X, y, accept_sparse='csr', dtype=_dtype,
|
230
|
+
|
231
|
+
1343 order="C",
|
232
|
+
|
233
|
+
-> 1344 accept_large_sparse=solver != 'liblinear')
|
234
|
+
|
235
|
+
1345 check_classification_targets(y)
|
236
|
+
|
237
|
+
1346 self.classes_ = np.unique(y)
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
/opt/anaconda3/lib/python3.7/site-packages/sklearn/base.py in _validate_data(self, X, y, reset, validate_separately, **check_params)
|
242
|
+
|
243
|
+
430 y = check_array(y, **check_y_params)
|
244
|
+
|
245
|
+
431 else:
|
246
|
+
|
247
|
+
--> 432 X, y = check_X_y(X, y, **check_params)
|
248
|
+
|
249
|
+
433 out = X, y
|
250
|
+
|
251
|
+
434
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
/opt/anaconda3/lib/python3.7/site-packages/sklearn/utils/validation.py in inner_f(*args, **kwargs)
|
256
|
+
|
257
|
+
70 FutureWarning)
|
258
|
+
|
259
|
+
71 kwargs.update({k: arg for k, arg in zip(sig.parameters, args)})
|
260
|
+
|
261
|
+
---> 72 return f(**kwargs)
|
262
|
+
|
263
|
+
73 return inner_f
|
264
|
+
|
265
|
+
74
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
/opt/anaconda3/lib/python3.7/site-packages/sklearn/utils/validation.py in check_X_y(X, y, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, multi_output, ensure_min_samples, ensure_min_features, y_numeric, estimator)
|
270
|
+
|
271
|
+
800 ensure_min_samples=ensure_min_samples,
|
272
|
+
|
273
|
+
801 ensure_min_features=ensure_min_features,
|
274
|
+
|
275
|
+
--> 802 estimator=estimator)
|
276
|
+
|
277
|
+
803 if multi_output:
|
278
|
+
|
279
|
+
804 y = check_array(y, accept_sparse='csr', force_all_finite=True,
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
/opt/anaconda3/lib/python3.7/site-packages/sklearn/utils/validation.py in inner_f(*args, **kwargs)
|
284
|
+
|
285
|
+
70 FutureWarning)
|
286
|
+
|
287
|
+
71 kwargs.update({k: arg for k, arg in zip(sig.parameters, args)})
|
288
|
+
|
289
|
+
---> 72 return f(**kwargs)
|
290
|
+
|
291
|
+
73 return inner_f
|
292
|
+
|
293
|
+
74
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
/opt/anaconda3/lib/python3.7/site-packages/sklearn/utils/validation.py in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, estimator)
|
298
|
+
|
299
|
+
596 array = array.astype(dtype, casting="unsafe", copy=False)
|
300
|
+
|
301
|
+
597 else:
|
302
|
+
|
303
|
+
--> 598 array = np.asarray(array, order=order, dtype=dtype)
|
304
|
+
|
305
|
+
599 except ComplexWarning:
|
306
|
+
|
307
|
+
600 raise ValueError("Complex data not supported\n"
|
308
|
+
|
309
|
+
|
310
|
+
|
311
|
+
/opt/anaconda3/lib/python3.7/site-packages/numpy/core/_asarray.py in asarray(a, dtype, order)
|
312
|
+
|
313
|
+
81
|
314
|
+
|
315
|
+
82 """
|
316
|
+
|
317
|
+
---> 83 return array(a, dtype, copy=False, order=order)
|
318
|
+
|
319
|
+
84
|
320
|
+
|
321
|
+
85
|
322
|
+
|
323
|
+
|
324
|
+
|
325
|
+
ValueError: setting an array element with a sequence.
|
326
|
+
|
327
|
+
```
|