下のようなコードで転移学習を行いたいです。
しかし、インプットの形⇒shape=(128, 48, 48, 1), dtype=float32
が原因でエラーが起きます。
どうすればよいでしょうか?
Python
1base_model = keras.applications.xception.Xception(weights="imagenet", 2 include_top=False) 3avg = keras.layers.GlobalAveragePooling2D()(base_model.output) 4output = keras.layers.Dense(len(emotions), activation="softmax")(avg) 5model = keras.models.Model(inputs=base_model.input, outputs=output)
上の行の1,2行目のところに、 input_tensor=Input(shape=(48, 48, 1))と指定したとことろ、カッコ内の1の部分が3でないとエラーが起きてしまいます。調べた所、そこはチャンネル数といって1だと白黒、3だとカラーだそうで、白黒の写真はインプットできないのでしょうか。
Python
1for layer in base_model.layers: 2 layer.trainable = False 3 4optimizer = keras.optimizers.SGD(learning_rate=0.2, momentum=0.9, decay=0.01) 5model.compile(optimizer=optimizer, loss='categorical_crossentropy', 6 metrics=['accuracy'], run_eagerly=True) 7 8earlystop = tf.keras.callbacks.EarlyStopping(patience=10, min_delta=1e-4, restore_best_weights=True) 9lr = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_loss', patience=3, verbose=1, factor=0.5, min_lr=1e-7) 10hist = model.fit(train_images, train_labels, 11 validation_data=(val_images, val_labels), 12 epochs=5, 13 batch_size=128, 14 callbacks=[earlystop, lr])
上記のコードを実行すると以下のようにエラーが起こります。解決策が知りたいです。
Python(エラー文)
1ValueError Traceback (most recent call last) 2<ipython-input-42-c94008e60e1c> in <module>() 3 12 epochs=5, 4 13 batch_size=128, 5---> 14 callbacks=[earlystop, lr]) 6 71 frames 8/usr/local/lib/python3.7/dist-packages/keras/engine/input_spec.py in assert_input_compatibility(input_spec, inputs, layer_name) 9 247 if value is not None and shape_as_list[int(axis)] not in {value, None}: 10 248 raise ValueError( 11--> 249 f'Input {input_index} of layer "{layer_name}" is ' 12 250 f'incompatible with the layer: expected axis {axis} ' 13 251 f'of input shape to have value {value}, ' 14 15ValueError: Exception encountered when calling layer "model_4" (type Functional). 16 17Input 0 of layer "block1_conv1" is incompatible with the layer: expected axis -1 of input shape to have value 3, but received input with shape (128, 48, 48, 1) 18 19Call arguments received: 20 • inputs=tf.Tensor(shape=(128, 48, 48, 1), dtype=float32) 21 • training=True 22 • mask=None
回答1件
あなたの回答
tips
プレビュー