0
votes
class MyModel(Model):

    def __init__(self,num_classes=1):
        super(MyModel, self).__init__()

        self.conv1=Convolution2D(filters=8,kernel_size=8,padding='same')
        self.batch_norm1=BatchNormalization()
        self.activation1=Activation('relu')
        self.conv2=Convolution2D(filters=16,kernel_size=8,activation='relu',padding='same')
        self.batch_norm2=BatchNormalization()
        self.activation2=Activation('relu')
        self.MaxPooling2D=MaxPooling2D(pool_size =(2, 2))
        self.Flatten=Flatten()
        self.dense1=Dense(16,activation='relu')
        self.dense2=Dense(num_classes,kernel_regularizer=regularizers.l2(0.4))

    def call(self,inputs):

        x=self.conv1(inputs)
        x=self.batch_norm1(x)
        x=self.activation1(x)
        x=self.conv2(x)
        x=self.batch_norm2(x)
        x=self.activation2(x)
        x=self.MaxPooling2D(x)
        x=self.Flatten(x)
        x=self.dense1(x)
        return self.dense2(x)

    def compute_output_shape(self, input_shape):

        shape = tf.TensorShape(input_shape).as_list()
        shape[-1] = self.num_classes
        return tf.TensorShape(shape)

model=MyModel()

adam=Adam(learning_rate=1e-4)

model.compile(optimizer=adam,loss="mse")

earlystopper = EarlyStopping(monitor='val_loss', patience=20, verbose=0) 

checkpoint =ModelCheckpoint("C:/Users/user/Desktop/research/pic_recognition/cnn2d-model.hdf5",save_best_only=True)

callback_list=[earlystopper,checkpoint]  

model.fit(x_train, y_train, epochs=50, batch_size=8,validation_split=0.1,callbacks=callback_list)

but I am getting this error:

File "", line 46, in model.fit(x_train, y_train, epochs=50, batch_size=8,validation_split=0.1,callbacks=callback_list) File "D:\Anaconda3\lib\site-packages\keras\engine\training.py", line 1239, in fit validation_freq=validation_freq) File "D:\Anaconda3\lib\site-packages\keras\engine\training_arrays.py", line 216, in fit_loop callbacks.on_epoch_end(epoch, epoch_logs) File "D:\Anaconda3\lib\site-packages\keras\callbacks\callbacks.py", line 152, in on_epoch_end callback.on_epoch_end(epoch, logs) File "D:\Anaconda3\lib\site-packages\keras\callbacks\callbacks.py", line 719, in on_epoch_end self.model.save(filepath, overwrite=True) File "D:\Anaconda3\lib\site-packages\keras\engine\network.py", line 1150, in save raise NotImplementedError NotImplementedError

1

1 Answers

0
votes

For custom models, you have to use "save_weights_only = True" for ModelCheckpoint() or use model.save_weights()

Refer to the below links for further details:

  1. https://github.com/tensorflow/tensorflow/issues/22837

  2. https://github.com/keras-team/keras/issues/12922