teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

エラーが出るまでの全てのプログラムを書きました。

2020/07/05 07:03

投稿

nifeP
nifeP

スコア1

title CHANGED
File without changes
body CHANGED
@@ -1,5 +1,90 @@
1
1
  機械学習で、画像認識をやろうと思っているのですが、画像が入ったファイルが2個までならできるのですが、3つ以上にするとエラーが起こってしまいます。
2
2
  ```python
3
+ !pip install icrawler
4
+ ```
5
+ ```python
6
+ # from icrawler.builtin import GoogleImageCrawler
7
+ # もつ煮の画像を100枚取得
8
+ # crawler = GoogleImageCrawler(storage={"root_dir": "moosuni"})
9
+ # crawler.crawl(keyword="もつ煮", max_num=100)
10
+ from icrawler.builtin import BingImageCrawler
11
+
12
+ # もつ煮の画像を100枚取得
13
+ crawler = BingImageCrawler(storage={"root_dir": "motsuni"})
14
+ crawler.crawl(keyword="もつ煮", max_num=100)
15
+ ```
16
+ ```python
17
+ # ナポリタンの画像を100枚取得
18
+ # crawler = GoogleImageCrawler(storage={"root_dir": "naporitan"})
19
+ # crawler.crawl(keyword="ナポリタン", max_num=100)
20
+ from icrawler.builtin import BingImageCrawler
21
+
22
+ # ナポリタンの画像を100枚取得
23
+ crawler = BingImageCrawler(storage={"root_dir": "naporitan"})
24
+ crawler.crawl(keyword="ナポリタン", max_num=100)
25
+ ```
26
+ ```python
27
+ # ラーメンの画像を100枚取得
28
+ # crawler = GoogleImageCrawler(storage={"root_dir": "ra-men"})
29
+ # crawler.crawl(keyword="ラーメン", max_num=100)
30
+ from icrawler.builtin import BingImageCrawler
31
+
32
+ # ラーメンの画像を100枚取得
33
+ crawler = BingImageCrawler(storage={"root_dir": "ra-men"})
34
+ crawler.crawl(keyword="醤油ラーメン", max_num=100)
35
+ ```
36
+ ```python
37
+ from PIL import Image
38
+ import os, glob
39
+ import numpy as np
40
+ from PIL import ImageFile
41
+ # IOError: image file is truncated (0 bytes not processed)回避のため
42
+ ImageFile.LOAD_TRUNCATED_IMAGES = True
43
+
44
+ classes = ["motsuni", "naporitan", "ra-men"]
45
+ num_classes = len(classes)
46
+ image_size = 64
47
+ num_testdata = 25
48
+
49
+ X_train = []
50
+ X_test = []
51
+ y_train = []
52
+ y_test = []
53
+
54
+ for index, classlabel in enumerate(classes):
55
+ photos_dir = "./" + classlabel
56
+ files = glob.glob(photos_dir + "/*.jpg")
57
+ for i, file in enumerate(files):
58
+ image = Image.open(file)
59
+ image = image.convert("RGB")
60
+ image = image.resize((image_size, image_size))
61
+ data = np.asarray(image)
62
+ if i < num_testdata:
63
+ X_test.append(data)
64
+ y_test.append(index)
65
+ else:
66
+
67
+ for angle in range(-20, 20, 5):
68
+
69
+ img_r = image.rotate(angle)
70
+ data = np.asarray(img_r)
71
+ X_train.append(data)
72
+ y_train.append(index)
73
+
74
+ img_trains = img_r.transpose(Image.FLIP_LEFT_RIGHT)
75
+ data = np.asarray(img_trains)
76
+ X_train.append(data)
77
+ y_train.append(index)
78
+
79
+ X_train = np.array(X_train)
80
+ X_test = np.array(X_test)
81
+ y_train = np.array(y_train)
82
+ y_test = np.array(y_test)
83
+
84
+ xy = (X_train, X_test, y_train, y_test)
85
+ np.save("./ryouri.npy", xy)
86
+ ```
87
+ ```python
3
88
  from keras.models import Sequential
4
89
  from keras.layers import Conv2D, MaxPooling2D
5
90
  from keras.layers import Activation, Dropout, Flatten, Dense
@@ -7,7 +92,7 @@
7
92
  import keras
8
93
  import numpy as np
9
94
 
10
- classes = ["motsuni", "naporitan", "jirou"]
95
+ classes = ["motsuni", "naporitan", "ra-men"]
11
96
  num_classes = len(classes)
12
97
  image_size = 64
13
98
 
@@ -102,4 +187,6 @@
102
187
 
103
188
  Google Colab でやっています。
104
189
 
190
+ 使う画像は、上のコードでウェブから拾ってきています。
191
+
105
192
  3つ以上のファイルを機械学習するためにはどのようにすればいいか、教えていただけると嬉しいです。