def init(): log.info('init called') sess = tf.InteractiveSession() loaded_model = MobileNet(input_shape=( size, size, 1), alpha=1., weights=None, classes=NCATS) loaded_model.load_weights('./doodle_classification/model/model.h5') loaded_model.compile(optimizer=Adam(lr=0.002), loss='categorical_crossentropy', metrics=[categorical_crossentropy, categorical_accuracy, top_3_accuracy]) # log.info(loaded_model.summary()) graph = tf.get_default_graph() return loaded_model, sess, graph
class mobilenet: def __init__(self, maxBin, maxBin2): """ must have self.model, self.params, and self._name """ self._name = 'MobileNet-imagenet' self.model = MobileNet(include_top=True, weights="imagenet", input_shape=(224, 224, 3), classes=1000) self.params = { 'age hidden layer': 512, 'gender hidden layer': 1024, 'ethnicity hidden layer': 1024, } self.model = addOutputLayers(self.model, maxBin, maxBin2, self.params) self.model.compile(optimizer='Adam', loss=losses, metrics=['accuracy']) def name(self): return self._name
model.summary() # model.trainable = False # model.save('C:/nmb/nmb_data/h5/5s/mobilenet_rmsprop_1.h5') # 컴파일, 훈련 op = RMSprop(lr=1e-3) batch_size = 8 es = EarlyStopping(monitor='val_loss', patience=20, restore_best_weights=True, verbose=1) lr = ReduceLROnPlateau(monitor='val_loss', vactor=0.5, patience=10, verbose=1) path = 'C:/nmb/nmb_data/h5/5s/mobilenet/mobilenet_rmsprop_1.h5' mc = ModelCheckpoint(path, monitor='val_loss', verbose=1, save_best_only=True) model.compile(optimizer=op, loss="sparse_categorical_crossentropy", metrics=['acc']) history = model.fit(x_train, y_train, epochs=1000, batch_size=batch_size, validation_split=0.2, callbacks=[es, lr, mc]) # 평가, 예측 model = load_model('C:\\nmb\\nmb_data\\h5\\5s_last\\mobilenet_rmsprop_1.h5') # model.load_weights('C:/nmb/nmb_data/h5/5s/mobilenet/mobilenet_rmsprop_1.h5') result = model.evaluate(x_test, y_test, batch_size=8) print("loss : {:.5f}".format(result[0])) print("acc : {:.5f}".format(result[1]))
model.save('C:/nmb/nmb_data/h5/5s/mobilenet/mobilenet_rmsprop_1.h5') # 컴파일, 훈련 op = RMSprop(lr=1e-3) batch_size = 8 es = EarlyStopping(monitor='val_loss', patience=20, restore_best_weights=True, verbose=1) lr = ReduceLROnPlateau(monitor='val_loss', vactor=0.5, patience=10, verbose=1) path = 'C:/nmb/nmb_data/h5/5s/mobilenet/mobilenet_rmsprop_1.h5' mc = ModelCheckpoint(path, monitor='val_loss', verbose=1, save_best_only=True) model.compile(optimizer=op, loss="sparse_categorical_crossentropy", metrics=['acc', f1_m, recall_m, precision_m]) history = model.fit(x_train, y_train, epochs=1000, batch_size=batch_size, validation_split=0.2, callbacks=[es, lr, mc]) # 평가, 예측 # model = load_model('C:/nmb/nmb_data/h5/5s/mobilenet/mobilenet_rmsprop_1.h5') model.load_weights('C:/nmb/nmb_data/h5/5s/mobilenet/mobilenet_rmsprop_1.h5') result = model.evaluate(x_test, y_test, batch_size=8) print("loss : {:.5f}".format(result[0])) print("acc : {:.5f}".format(result[1])) print("f1_score : {:.5f}".format(result[2]))
""" Source: https://github.com/benhamner/Metrics/blob/master/Python/ml_metrics/average_precision.py """ return np.mean([apk(a, p, k) for a, p in zip(actual, predicted)]) def preds2catids(predictions): return pd.DataFrame(np.argsort(-predictions, axis=1)[:, :3], columns=['a', 'b', 'c']) def top_3_accuracy(y_true, y_pred): return top_k_categorical_accuracy(y_true, y_pred, k=3) STEPS = 800 EPOCHS = 16 size = 64 batchsize = 680 model = MobileNet(input_shape=(size, size, 1), alpha=1., weights=None, classes=NCATS) model.compile(optimizer=Adam(lr=0.002), loss='categorical_crossentropy', metrics=[categorical_crossentropy, categorical_accuracy, top_3_accuracy]) print(model.summary()) def draw_cv2(raw_strokes, size=256, lw=6, time_color=True): img = np.zeros((BASE_SIZE, BASE_SIZE), np.uint8) for t, stroke in enumerate(raw_strokes): for i in range(len(stroke[0]) - 1): color = 255 - min(t, 10) * 13 if time_color else 255 _ = cv2.line(img, (stroke[0][i], stroke[1][i]), (stroke[0][i + 1], stroke[1][i + 1]), color, lw) if size != BASE_SIZE: return cv2.resize(img, (size, size)) else: return img def image_generator_xd(size, batchsize, ks, lw=6, time_color=True): while True:
def mobilenet_seg_model(pretrained_weights=None, input_size=(256, 256, 3)): model = MobileNet(input_shape=input_size, weights='imagenet', include_top=False) x = model.output encoder = Model(inputs=model.input, outputs=x) skip_layer_1 = encoder.get_layer('conv_pw_1_relu').output skip_layer_2 = encoder.get_layer('conv_pw_3_relu').output skip_layer_3 = encoder.get_layer('conv_pw_5_relu').output skip_layer_4 = encoder.get_layer('conv_pw_11_relu').output up6 = Conv2D( 512, 2, activation='relu', padding='same', kernel_initializer='he_normal')( x) #(UpSampling2D(size = (2,2), interpolation='bilinear')(x)) up6 = Conv2DTranspose(512, 2, activation='relu', padding='same', strides=2, kernel_initializer='he_normal')(up6) merge6 = concatenate([skip_layer_4, up6], axis=3) conv6 = Conv2D(512, 3, activation='relu', padding='same', kernel_initializer='he_normal')(merge6) conv6 = Conv2D(512, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv6) up7 = Conv2D( 256, 2, activation='relu', padding='same', kernel_initializer='he_normal' )(conv6) #(UpSampling2D(size = (2,2), interpolation='bilinear')(conv6)) up7 = Conv2DTranspose(256, 2, activation='relu', padding='same', strides=2, kernel_initializer='he_normal')(up7) merge7 = concatenate([skip_layer_3, up7], axis=3) conv7 = Conv2D(256, 3, activation='relu', padding='same', kernel_initializer='he_normal')(merge7) conv7 = Conv2D(256, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv7) up8 = Conv2D( 128, 2, activation='relu', padding='same', kernel_initializer='he_normal' )(conv7) #(UpSampling2D(size = (2,2), interpolation='bilinear')(conv7)) up8 = Conv2DTranspose(128, 2, activation='relu', padding='same', strides=2, kernel_initializer='he_normal')(up8) merge8 = concatenate([skip_layer_2, up8], axis=3) conv8 = Conv2D(128, 3, activation='relu', padding='same', kernel_initializer='he_normal')(merge8) conv8 = Conv2D(128, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv8) up9 = Conv2D( 64, 2, activation='relu', padding='same', kernel_initializer='he_normal' )(conv8) #(UpSampling2D(size = (2,2), interpolation='bilinear')(conv8)) up9 = Conv2DTranspose(64, 2, activation='relu', padding='same', strides=2, kernel_initializer='he_normal')(up9) merge9 = concatenate([skip_layer_1, up9], axis=3) conv9 = Conv2D(64, 3, activation='relu', padding='same', kernel_initializer='he_normal')(merge9) conv9 = Conv2D(64, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv9) up10 = Conv2D( 32, 2, activation='relu', padding='same', kernel_initializer='he_normal' )(conv9) #(UpSampling2D(size = (2,2), interpolation='bilinear')(conv9)) up10 = Conv2DTranspose(32, 2, activation='relu', padding='same', strides=2, kernel_initializer='he_normal')(up10) #merge9 = concatenate([conv1,up9], axis = 3) conv10 = Conv2D(32, 3, activation='relu', padding='same', kernel_initializer='he_normal')(up10) conv10 = Conv2D(32, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv10) conv10 = Conv2D(16, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv10) conv10 = Conv2D(1, 1, activation='sigmoid')(conv10) model = Model(inputs=model.input, outputs=conv10) #model.compile(optimizer = Adam(lr = 1e-4), loss = 'binary_crossentropy', metrics=[my_IoU]) #model.compile(optimizer = Adam(lr = 1e-4), loss = dice_coef_loss, metrics=[my_IoU]) model.compile(optimizer=Adam(lr=1e-4), loss=weighted_bce_dice_loss, metrics=[my_IoU]) model.summary() if (pretrained_weights is not None): model.load_weights(pretrained_weights) return model
def build_model(mode, model_name=None, model_path=None): clear_session() if mode == 'train': img = Input( shape=(96, 96, 3)) # ResNet50 minimum size (32,32) for others (128,128) if model_name == 'DenseNet121': #Checked and Working model = DenseNet121(include_top=False, weights='imagenet', input_tensor=img, input_shape=None, pooling='avg') elif model_name == 'MobileNet': #checked, raised shape error, #Error Resolved, Now working model = MobileNet(include_top=True, weights='imagenet', input_tensor=img, input_shape=None, pooling='avg') elif model_name == 'Xception': #Checked and Working model = Xception(include_top=False, weights='imagenet', input_tensor=img, input_shape=None, pooling='max') elif model_name == 'ResNet50': #Image Dimesion size should be high eg 224x224, not sufficient GPU memory resource model = ResNet50(include_top=False, weights='imagenet', input_tensor=img, input_shape=None, pooling='avg') elif model_name == 'InceptionV3': #Checked and Working model = InceptionV3(include_top=False, weights='imagenet', input_tensor=img, input_shape=(None), pooling='avg') elif model_name == 'VGG19': #to be checked model = InceptionV4(include_top=False, weights='imagenet', input_tensor=img, input_shape=None, pooling='avg') elif model_name == 'VGG16': #Checked and Working model = VGG16(include_top=False, weights='imagenet', input_tensor=img, input_shape=(None), pooling='max') elif model_name == 'VGG19': #to be checked model = VGG19(include_top=False, weights='imagenet', input_tensor=img, input_shape=None, pooling='avg') final_layer = model.layers[-1].output dense_layer_1 = Dense(128, activation='relu')(final_layer) output_layer = Dense(4, activation='softmax')(dense_layer_1) model = Model(inputs=[img], outputs=output_layer) model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) elif mode == 'inference': model = load_model(model_path) return model