1つの入力から4クラスの分類を行なう場合について(カテゴリ分類)
from tensorflow.keras.layers import Conv2D
from keras.layers import Dense, BatchNormalization
from keras.layers.pooling import MaxPooling2D
from keras.layers.core import Dense, Dropout, Flatten
from keras.models import Sequential
from tensorflow import keras
model = Sequential()
model.add(Conv2D(20, kernel_size=5, strides=2, activation='relu',input_shape=(120,128,1)))
model.add(MaxPooling2D(3, strides=2))
model.add(BatchNormalization())
model.add(Conv2D(50, kernel_size=5, strides=2, activation='relu'))
model.add(MaxPooling2D(3, strides=2))
model.add(BatchNormalization())
model.add(Flatten())
model.add(Dense(100, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(4, activation='softmax'))
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
from sklearn.preprocessing import LabelEncoder
import numpy as np
code = np.array(labels['expression'])
label_encoder = LabelEncoder()
vec = label_encoder.fit_transform(code)
one_hot_labels = keras.utils.to_categorical(vec, num_classes=10)
model.fit(col, one_hot_labels, epochs=10, batch_size=32)
//////////////////////////////////////////////////////////
col.shape=(312,120,128)
one_hot_label.shape=(312,4)
/////////////////////////////////////////////////////////
model.summary()
Model: "sequential_6"
Layer (type) Output Shape Param
conv2d_9 (Conv2D) (None, 58, 62, 20) 520
max_pooling2d_6 (MaxPooling2 (None, 28, 30, 20) 0
batch_normalization_6 (Batch (None, 28, 30, 20) 80
conv2d_10 (Conv2D) (None, 12, 13, 50) 25050
max_pooling2d_7 (MaxPooling2 (None, 5, 6, 50) 0
batch_normalization_7 (Batch (None, 5, 6, 50) 200
flatten_4 (Flatten) (None, 1500) 0
dense_6 (Dense) (None, 100) 150100
dropout_3 (Dropout) (None, 100) 0
dense_7 (Dense) (None, 4) 404
Total params: 176,354
Trainable params: 176,214
Non-trainable params: 140
/////////////////////////////////////////////////////////
colはモノクロ画像を読み込んでnp.arrayで格納しています
回答1件
あなたの回答
tips
プレビュー