質問編集履歴

1

質問の具体化

2021/03/20 10:15

投稿

rest
rest

スコア18

test CHANGED
File without changes
test CHANGED
@@ -9,145 +9,3 @@
9
9
  ② rgbaの画像がグレースケールに変換されて学習される。
10
10
 
11
11
  の二つを考えています。どちらだと思いますか?
12
-
13
-
14
-
15
- ・参考にしたサイト
16
-
17
- [VGG16を転移学習させて「まどか☆マギカ」のキャラを見分ける](https://qiita.com/God_KonaBanana/items/2cf829172087d2423f58)
18
-
19
-
20
-
21
- ・全文
22
-
23
- ```ここに言語を入力
24
-
25
- #model&train
26
-
27
- from keras.models import Model
28
-
29
- from keras.layers import Dense, GlobalAveragePooling2D,Input
30
-
31
- from keras.applications.vgg16 import VGG16
32
-
33
- from keras.preprocessing.image import ImageDataGenerator
34
-
35
- from keras.optimizers import SGD
36
-
37
- from keras.callbacks import CSVLogger
38
-
39
- import matplotlib.pyplot as plt
40
-
41
- import os
42
-
43
-
44
-
45
- classes = ['hituji','buta','usi']
46
-
47
- label=['hituji','buta','usi']
48
-
49
- img_height=256
50
-
51
- img_width=256
52
-
53
- batch_size=16
54
-
55
- num_epochs=50
56
-
57
- n_categories=3
58
-
59
- seed=1
60
-
61
- file_name = 'doubutu_bunrui'
62
-
63
- print(file_name)
64
-
65
-
66
-
67
- train_dir==os.path.join('D:','train')
68
-
69
-
70
-
71
- #model作成(グレースケール)
72
-
73
- base_model=VGG16(weights=None,include_top=False,
74
-
75
- input_shape=(img_width,img_height,1),
76
-
77
- input_tensor=Input(shape=(img_width,img_height,1)),
78
-
79
- )
80
-
81
-
82
-
83
- #add new layers instead of FC networks
84
-
85
- x=base_model.output
86
-
87
- x=GlobalAveragePooling2D()(x)
88
-
89
- x=Dense(1024,activation='relu')(x)
90
-
91
- prediction=Dense(n_categories,activation='softmax')(x)
92
-
93
- model=Model(inputs=base_model.input,outputs=prediction)
94
-
95
- #fix weights
96
-
97
- for layer in base_model.layers[:0]:
98
-
99
- layer.trainable=False
100
-
101
- model.compile(optimizer=SGD(lr=0.0001,momentum=0.9),
102
-
103
- loss='categorical_crossentropy',
104
-
105
- metrics=['accuracy'])
106
-
107
- #save model
108
-
109
- json_string=model.to_json()
110
-
111
- open(file_name+'.json','w').write(json_string)
112
-
113
-
114
-
115
- #学習(train)
116
-
117
- train_datagen=ImageDataGenerator()
118
-
119
- train_generator=train_datagen.flow_from_directory(
120
-
121
- train_dir,
122
-
123
- target_size=(img_width,img_height),
124
-
125
- batch_size=batch_size,
126
-
127
- classes=classes,
128
-
129
- class_mode='categorical',
130
-
131
- color_mode='grayscale'
132
-
133
- shuffle=True,
134
-
135
- seed=seed
136
-
137
- )
138
-
139
- #history
140
-
141
- history=model.fit_generator(train_generator,
142
-
143
- epochs=num_epochs,
144
-
145
- verbose=0,
146
-
147
- callbacks=[CSVLogger(file_name+'.csv')])
148
-
149
- #save weights
150
-
151
- model.save(file_name+'.h5')
152
-
153
- ```