def extractImgFeature(self, filename, modelType): if modelType == 'inceptionv3': from tensorflow.keras.applications.inception_v3 import preprocess_input target_size = (299, 299) model = InceptionV3() elif modelType == 'xception': from tensorflow.keras.applications.xception import preprocess_input target_size = (299, 299) model = Xception() elif modelType == 'vgg16': from tensorflow.keras.applications.vgg16 import preprocess_input target_size = (224, 224) model = VGG16() elif modelType == 'resnet50': from tensorflow.keras.applications.resnet50 import preprocess_input target_size = (224, 224) model = ResNet50() model.layers.pop() model = Model(inputs=model.inputs, outputs=model.layers[-1].output) image = load_img(filename, target_size=target_size) # Loading and resizing image image = img_to_array( image) # Convert the image pixels to a numpy array image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2])) # Reshape data for the model image = preprocess_input( image) # Prepare the image for the CNN Model model features = model.predict( image, verbose=0) # Pass image into model to get encoded features return features
def yolo3_spp_xception_body(inputs, num_anchors, num_classes): """Create YOLO_V3 SPP Xception model CNN body in Keras.""" xception = Xception(input_tensor=inputs, weights='imagenet', include_top=False) # input: 416 x 416 x 3 # block14_sepconv2_act: 13 x 13 x 2048 # block13_sepconv2_bn(middle in block13): 26 x 26 x 1024 # add_46(end of block12): 26 x 26 x 728 # block4_sepconv2_bn(middle in block4) : 52 x 52 x 728 # add_37(end of block3) : 52 x 52 x 256 f1 = xception.get_layer('block14_sepconv2_act').output # f1 :13 x 13 x 2048 x, y1 = make_spp_last_layers(f1, 1024, num_anchors * (num_classes + 5)) x = compose(DarknetConv2D_BN_Leaky(512, (1, 1)), UpSampling2D(2))(x) f2 = xception.get_layer('block13_sepconv2_bn').output # f2: 26 x 26 x 1024 x = Concatenate()([x, f2]) x, y2 = make_last_layers(x, 512, num_anchors * (num_classes + 5)) x = compose(DarknetConv2D_BN_Leaky(256, (1, 1)), UpSampling2D(2))(x) f3 = xception.get_layer('block4_sepconv2_bn').output # f3 : 52 x 52 x 728 x = Concatenate()([x, f3]) x, y3 = make_last_layers(x, 256, num_anchors * (num_classes + 5)) return Model(inputs=inputs, outputs=[y1, y2, y3])
def yolo3lite_xception_body(inputs, num_anchors, num_classes): '''Create YOLO_v3 Lite Xception model CNN body in keras.''' xception = Xception(input_tensor=inputs, weights='imagenet', include_top=False) print('backbone layers number: {}'.format(len(xception.layers))) # input: 416 x 416 x 3 # block14_sepconv2_act: 13 x 13 x 2048 # block13_sepconv2_bn(middle in block13): 26 x 26 x 1024 # add_46(end of block12): 26 x 26 x 728 # block4_sepconv2_bn(middle in block4) : 52 x 52 x 728 # add_37(end of block3) : 52 x 52 x 256 # f1: 13 x 13 x 2048 f1 = xception.get_layer('block14_sepconv2_act').output # f2: 26 x 26 x 1024 f2 = xception.get_layer('block13_sepconv2_bn').output # f3: 52 x 52 x 728 f3 = xception.get_layer('block4_sepconv2_bn').output #f1_channel_num = 2048 #f2_channel_num = 1024 #f3_channel_num = 728 f1_channel_num = 1024 f2_channel_num = 512 f3_channel_num = 256 y1, y2, y3 = yolo3lite_predictions((f1, f2, f3), (f1_channel_num, f2_channel_num, f3_channel_num), num_anchors, num_classes) return Model(inputs = inputs, outputs=[y1,y2,y3])
def tiny_yolo3_xception_body(inputs, num_anchors, num_classes): '''Create Tiny YOLO_v3 Xception model CNN body in keras.''' xception = Xception(input_tensor=inputs, weights='imagenet', include_top=False) # input: 416 x 416 x 3 # block14_sepconv2_act: 13 x 13 x 2048 # block13_sepconv2_bn(middle in block13): 26 x 26 x 1024 # add_46(end of block12): 26 x 26 x 728 # block4_sepconv2_bn(middle in block4) : 52 x 52 x 728 # add_37(end of block3) : 52 x 52 x 256 x1 = xception.get_layer('block13_sepconv2_bn').output # x1 :26 x 26 x 1024 x2 = xception.get_layer('block14_sepconv2_act').output # x2 :13 x 13 x 2048 x2 = DarknetConv2D_BN_Leaky(1024, (1, 1))(x2) y1 = compose( DarknetConv2D_BN_Leaky(2048, (3, 3)), #Depthwise_Separable_Conv2D_BN_Leaky(filters=2048, kernel_size=(3, 3), block_id_str='14'), DarknetConv2D(num_anchors * (num_classes + 5), (1, 1)))(x2) x2 = compose(DarknetConv2D_BN_Leaky(512, (1, 1)), UpSampling2D(2))(x2) y2 = compose( Concatenate(), DarknetConv2D_BN_Leaky(1024, (3, 3)), #Depthwise_Separable_Conv2D_BN_Leaky(filters=1024, kernel_size=(3, 3), block_id_str='15'), DarknetConv2D(num_anchors * (num_classes + 5), (1, 1)))([x2, x1]) return Model(inputs, [y1, y2])
def build_models(): # Model building # Feature extraction layers - transfer learning # input shape keras_input_shape = (KERAS_IMG_SIZE[0], KERAS_IMG_SIZE[1], 3) keras_input = Input(shape=keras_input_shape) base_model = Xception( include_top=False, # no dense layers in the end to classify so we can make our own layer weights=FEATURE_WEIGHTS_PATH, input_shape=keras_input_shape ) base_model.trainable = False # Classifier x = base_model(keras_input, training=False) x = GlobalAveragePooling2D()(x) classifier_output = Dense(2, activation='softmax')(x) classifier = Model(keras_input, classifier_output) # Regressor regressor_output = Dense(4, activation='sigmoid')(x) regressor = Model(keras_input, regressor_output) return classifier, regressor
def tiny_yolo3lite_xception_body(inputs, num_anchors, num_classes): '''Create Tiny YOLO_v3 Lite Xception model CNN body in keras.''' xception = Xception(input_tensor=inputs, weights='imagenet', include_top=False) # input: 416 x 416 x 3 # block14_sepconv2_act: 13 x 13 x 2048 # block13_sepconv2_bn(middle in block13): 26 x 26 x 1024 # add_46(end of block12): 26 x 26 x 728 # block4_sepconv2_bn(middle in block4) : 52 x 52 x 728 # add_37(end of block3) : 52 x 52 x 256 # f1 :13 x 13 x 2048 f1 = xception.get_layer('block14_sepconv2_act').output # f2 :26 x 26 x 1024 f2 = xception.get_layer('block13_sepconv2_bn').output f1_channel_num = 2048 f2_channel_num = 1024 #f1_channel_num = 1024 #f2_channel_num = 512 y1, y2 = tiny_yolo3lite_predictions( (f1, f2), (f1_channel_num, f2_channel_num), num_anchors, num_classes) return Model(inputs, [y1, y2])
def build_xception(height, width, depth, include_top=False, weights='imagenet'): xception_model = Xception(weights=weights, include_top=include_top, input_shape=(height, width, depth)) model = Sequential() model.add(xception_model) model.add(Flatten()) model.add(Dense(512)) model.add(LeakyReLU()) model.add(BatchNormalization()) model.add(Dropout(0.4)) model.add(Dense(256)) model.add(LeakyReLU()) model.add(BatchNormalization()) model.add(Dropout(0.4)) model.add(Dense(128)) model.add(LeakyReLU()) model.add(BatchNormalization()) model.add(Dropout(0.4)) model.add(Dense(2)) model.add(Activation("softmax")) xception_model.trainable = False return model
class XceptionExtractor: ''' ''' def __init__(self): ''' ''' self.input_shape = (299, 299, 3) self.model = Xception( weights='imagenet', input_shape=self.input_shape, pooling='avg', include_top=False, ) self.model.predict(zeros((1, 299, 299, 3))) def extract(self, img_path): ''' ''' img = image.load_img(img_path, target_size=(self.input_shape[0], self.input_shape[1])) img = image.img_to_array(img) img = expand_dims(img, axis=0) img = preprocess_input(img) feat = self.model.predict(img) norm_feat = feat[0] / linalg.norm(feat[0]) return [i.item() for i in norm_feat]
def pretrain_model(): model =Xception(include_top = False, weights = "imagenet",input_shape = (71,71,3)) x=model.output x=GlobalAveragePooling2D()(x) x=Dense(512,activation='relu')(x) x = Dropout(0.8)(x) x= Dense(256,activation='relu')(x) x = Dropout(0.8)(x) y=Dense(10,activation='softmax')(x) model = Model(inputs = model.input, outputs =y) for layer in model.layers[:30]: layer.trainable = False if layer.name.startswith('batch_normalization'): layer.trainable = True for layer in model.layers[30:]: layer.trainable = True #if layer.name.startswith('batch_normalization'): # layer.trainable = True model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=["accuracy"]) return model
def prediction(self, update, context): """ Prediction method that using pre-trained on ImageNet dataset Xception model for recognizing sent user's picture returns PICTURE that means switch to picture() method you can infinitely send images to bot until you print /cancel or kill bot process by """ id = str(update.message.from_user.id) img = image.load_img('userID_{}_photo.jpg'.format(id), target_size=(299, 299)) update.message.reply_text('[INFO] Preprocessing...') x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) update.message.reply_text('[INFO] Recognizing...') model = Xception() preds = model.predict(x) decoded_preds = decode_predictions(preds, top=1)[0][0] update.message.reply_text('Predicted: {} with {:.2f}% accuracy'.format( decoded_preds[1], decoded_preds[2] * 100)) update.message.reply_text( 'Send me another image or /cancel for stop conversation') return self.PICTURE
def yolo3_spp_xception_body(inputs, num_anchors, num_classes): """Create YOLO_V3 SPP Xception model CNN body in Keras.""" xception = Xception(input_tensor=inputs, weights='imagenet', include_top=False) # input: 416 x 416 x 3 # block14_sepconv2_act: 13 x 13 x 2048 # block13_sepconv2_bn(middle in block13): 26 x 26 x 1024 # add_46(end of block12): 26 x 26 x 728 # block4_sepconv2_bn(middle in block4) : 52 x 52 x 728 # add_37(end of block3) : 52 x 52 x 256 # f1: 13 x 13 x 2048 f1 = xception.get_layer('block14_sepconv2_act').output # f2: 26 x 26 x 1024 f2 = xception.get_layer('block13_sepconv2_bn').output # f3: 52 x 52 x 728 f3 = xception.get_layer('block4_sepconv2_bn').output #f1_channel_num = 2048 #f2_channel_num = 1024 #f3_channel_num = 728 f1_channel_num = 1024 f2_channel_num = 512 f3_channel_num = 256 y1, y2, y3 = yolo3_predictions( (f1, f2, f3), (f1_channel_num, f2_channel_num, f3_channel_num), num_anchors, num_classes, use_spp=True) return Model(inputs=inputs, outputs=[y1, y2, y3])
def build_model(config): """Builds a keras model using Xception network as core and following instructions on the configuration file. Args: config (dict): Configuration dictionary Returns: keras.model: Keras model """ input_shape = config["model"]["input_shape"] + [3] i = Input( input_shape, name="model_input", ) x = preprocess_input(i) core = Xception(input_shape=input_shape, include_top=False, pooling="avg") if config["model"]["freeze_convolutional_layers"]: print("Freezing convolutional layers") core.trainable = False x = core(x) outputs = [] for clf_layer in config["model"]["target_encoder"]: n_classes = len(config["model"]["target_encoder"][clf_layer]) outputs.append( Dense(units=n_classes, activation="softmax", name=f"{clf_layer}_clf")(x)) model = Model(inputs=i, outputs=outputs) return model
def create_model(self): input_layer = tf.keras.layers.Input(self.input_dim) ## Uncomment lines according to model type # If training Xception, use first line. If training InceptionResNetV2, use second line, etc. model = Xception(input_tensor=input_layer, include_top=False, weights='imagenet') # model = InceptionResNetV2(input_tensor=input_layer, include_top=False, weights='imagenet') # model = EfficientNetB3(input_tensor=input_layer, include_top=False, weights='imagenet') # model = EfficientNetB5(input_tensor=input_layer, include_top=False, weights='imagenet') x = model(input_layer) # output layers x1 = tf.keras.layers.GlobalAveragePooling2D()(x) x2 = tf.keras.layers.GlobalMaxPooling2D()(x) x3 = tf.keras.layers.Flatten()(x) out = tf.keras.layers.Concatenate(axis=-1)([x1, x2, x3]) out = tf.keras.layers.Dropout(0.3)(out) output_layer = tf.keras.layers.Dense(self.n_classes, activation='softmax')(out) model = tf.keras.models.Model(inputs=input_layer, outputs=output_layer) model.compile(optimizer=tf.keras.optimizers.Adam(), loss="categorical_crossentropy", metrics=['acc']) return model
def yolo3lite_xception_body(inputs, num_anchors, num_classes): '''Create YOLO_v3 Lite Xception model CNN body in keras.''' xception = Xception(input_tensor=inputs, weights='imagenet', include_top=False) # input: 416 x 416 x 3 # block14_sepconv2_act: 13 x 13 x 2048 # block13_sepconv2_bn(middle in block13): 26 x 26 x 1024 # add_46(end of block12): 26 x 26 x 728 # block4_sepconv2_bn(middle in block4) : 52 x 52 x 728 # add_37(end of block3) : 52 x 52 x 256 # f1: 13 x 13 x 2048 f1 = xception.get_layer('block14_sepconv2_act').output # f2: 26 x 26 x 1024 f2 = xception.get_layer('block13_sepconv2_bn').output # f3: 52 x 52 x 728 f3 = xception.get_layer('block4_sepconv2_bn').output #f1_channel_num = 2048 #f2_channel_num = 1024 #f3_channel_num = 728 f1_channel_num = 1024 f2_channel_num = 512 f3_channel_num = 256 #feature map 1 head & output (13x13 for 416 input) x, y1 = make_depthwise_separable_last_layers(f1, f1_channel_num // 2, num_anchors * (num_classes + 5), block_id_str='14') #upsample fpn merge for feature map 1 & 2 x = compose(DarknetConv2D_BN_Leaky(f2_channel_num // 2, (1, 1)), UpSampling2D(2))(x) x = Concatenate()([x, f2]) #feature map 2 head & output (26x26 for 416 input) x, y2 = make_depthwise_separable_last_layers(x, f2_channel_num // 2, num_anchors * (num_classes + 5), block_id_str='15') #upsample fpn merge for feature map 2 & 3 x = compose(DarknetConv2D_BN_Leaky(f3_channel_num // 2, (1, 1)), UpSampling2D(2))(x) x = Concatenate()([x, f3]) #feature map 3 head & output (52x52 for 416 input) x, y3 = make_depthwise_separable_last_layers(x, f3_channel_num // 2, num_anchors * (num_classes + 5), block_id_str='16') return Model(inputs=inputs, outputs=[y1, y2, y3])
def train_model(self): """ Training the model """ print("Training the model") LR = 1e-3 epochs = 200 callbacks = [ EarlyStopping(monitor='val_loss', min_delta=0, patience=30, verbose=0, mode='auto'), ModelCheckpoint('model.h5', monitor='val_loss', mode='min', save_best_only=True), ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=10, verbose=0, mode='auto', min_delta=0.0001, cooldown=0, min_lr=0) ] # Pre trained model Xception without fully connected layers base_model = Xception(input_shape=(self.img_size[0], self.img_size[1], 3), include_top=False, weights='imagenet') # Unfreeze the layers base_model.trainable = True x = GlobalMaxPooling2D()(base_model.output) x = Dense(512, activation='relu')(x) x = Dense(10, activation='relu')(x) output = Dense(1, activation='linear')(x) model = Model(inputs=base_model.input, outputs=output) model.compile(loss='mse', optimizer=Adam(learning_rate=LR), metrics=[self.mae_in_months]) print(base_model.summary()) print(model.summary()) history = model.fit_generator( self.train_datagen.flow(self.x_train, self.y_train, batch_size=self.batch_size), steps_per_epoch=len(self.x_train) / self.batch_size, validation_data=self.val_datagen.flow(self.x_val, self.y_val, batch_size=self.batch_size), validation_steps=len(self.x_val) / self.batch_size, callbacks=callbacks, epochs=epochs, verbose=1) self.plot_it(history) model.load_weights('model.h5') pred = self.mean_bone_age + self.std_bone_age * (model.predict( self.x_val, batch_size=self.batch_size, verbose=True)) actual = self.mean_bone_age + self.std_bone_age * (self.y_val)
def get_kernel(params): model = Xception(weights=None) optimizer = SGD() model.compile( loss="sparse_categorical_crossentropy", optimizer=optimizer, metrics=["accuracy"], ) return model
def model(self, trainable=False): model = Xception(include_top=False, weights='imagenet') model.trainable = trainable inputs = tf.keras.Input(shape=(150, 150, 3)) x = model(inputs, training=trainable) x = tf.keras.layers.GlobalAveragePooling2D()(x) outputs = tf.keras.layers.Dense(1)(x) return tf.keras.Model(inputs, outputs)
def get_xception(): """Returns a Xception pretrained neural net. The function returns a partially pretrained Xception neural net. Returns: A modified Xception semi-pre-trained NN instance """ model = Xception(include_top=False) model.trainable = False core_output = model.layers[45].output # Weighting Xception output via channel and spatial Attention channel_attention_map = channel_attention(core_output) channel_weighted = core_output * channel_attention_map spatial_attention_map = spatial_attention(channel_weighted) core_output = channel_weighted * spatial_attention_map for _ in range(5): output = relu(core_output) output = SeparableConvolution2D(728, (3, 3), padding='same', depthwise_regularizer=L2(0.2), pointwise_regularizer=L2(0.03))(output) output = BatchNormalization()(output) output = Dropout(0.3)(output) output = relu(output) output = SeparableConvolution2D(728, (3, 3), padding='same', depthwise_regularizer=L2(0.2), pointwise_regularizer=L2(0.03))(output) output = BatchNormalization()(output) output = Dropout(0.3)(output) core_output = Add()[output, core_output] # Output Weighting via Attention channel_attention_map = channel_attention(core_output) channel_weighted = core_output * channel_attention_map spatial_attention_map = spatial_attention(channel_weighted) core_output = channel_weighted * spatial_attention_map model_output = GlobalAvgPool2D()(core_output) model_output = Dense(1, activation='sigmoid')(model_output) model = Model(inputs=model.input, outputs=model_output) return model
def __init__(self): ''' ''' self.input_shape = (299, 299, 3) self.model = Xception( weights='imagenet', input_shape=self.input_shape, pooling='avg', include_top=False, ) self.model.predict(zeros((1, 299, 299, 3)))
def single_feature_extract(imageArray) : model = Xception() # model = ResNet50(weights='imagenet') model.layers.pop() model = Model(inputs=model.inputs, outputs=model.layers[-2].output) image = cv2.resize(imageArray,((model.input_shape[1],model.input_shape[2]))) # image = load_img(filename,target_size=(model.input_shape[1],model.input_shape[2])) image = img_to_array(image) image = np.expand_dims(image,axis=0) image = preprocess_input(image) feature = model.predict(image) return feature
def extract_features(path): img = load_img(path, target_size=(299, 299)) img = img_to_array(img) plt.imshow(img / 255.) # img=mpimg.imread(path) image = np.expand_dims(img, axis=0) image = preprocess_input(image) model = Xception(weights='imagenet') model = Model(inputs=model.inputs, outputs=model.layers[-2].output) feat = model.predict(image) plt.show() return feat
def compute_net(self, features): """ Compute Xception depending on mode """ if self.mode == 'train_from_scratch' or self.mode == 'retrain': model_graph = Xception(weights=None, include_top=False) if self.mode == 'transfer': model_graph = Xception(include_top=False) for layer in model_graph.layers: layer.trainable = False layer = model_graph(features) layer = GlobalAveragePooling2D()(layer) return layer
def load_cnn_model(): """ Loads the Xception pre-trained CNN model :return: Xception model """ base_model = Xception(weights='imagenet', include_top=True) # get the feature outputs of second-to-last layer (final FC layer) outputs = base_model.get_layer('avg_pool').output cnn_model = Model(inputs=base_model.input, outputs=outputs) return cnn_model
def featuresExtraction(self, dataset, modelType): print('Generating image features using ' + str(configuration['CNNmodelType']) + ' model...') if modelType == 'inceptionv3': from tensorflow.keras.applications.inception_v3 import preprocess_input target_size = (299, 299) model = InceptionV3() elif modelType == 'xception': from tensorflow.keras.applications.xception import preprocess_input target_size = (299, 299) model = Xception() elif modelType == 'vgg16': from tensorflow.keras.applications.vgg16 import preprocess_input target_size = (224, 224) model = VGG16() elif modelType == 'resnet50': from tensorflow.keras.applications.resnet50 import preprocess_input target_size = (224, 224) model = ResNet50() else: print("please select a appropriate model") model.layers.pop( ) # Removing the last layer from the loaded model because we dont have to classify the images model = Model(inputs=model.inputs, outputs=model.layers[-1].output) features = dict() for name in tqdm(os.listdir(dataset)): filename = dataset + name image = load_img( filename, target_size=target_size) # Loading and resizing image image = img_to_array( image) # Convert the image pixels to a numpy array image = image.reshape( (1, image.shape[0], image.shape[1], image.shape[2])) # Reshape the indexs of data for the model image = preprocess_input( image ) # Preprocess the image inorder to pass the image to the CNN Model model feature = model.predict( image, verbose=0) # Pass image into model to get features imageId = name.split('.')[0] # Store the features features[imageId] = feature dump( features, open( configuration['featuresPath'] + 'features_' + str(configuration['CNNmodelType']) + '.pkl', 'wb')) print("length of total features: ", len(features))
def create_model(input_shape, config): input_tensor = Input( shape=input_shape) # this assumes K.image_dim_ordering() == 'tf' xception_model = Xception(include_top=False, weights=None, input_tensor=input_tensor) print(xception_model.summary()) x = xception_model.output x = GlobalAveragePooling2D()(x) predictions = Dense(config["num_classes"], activation="softmax")(x) return Model(input=xception_model.input, output=predictions)
def export_keras_model(): if not os.path.exists(KERAS_MODEL_PATH): model = Xception(weights='imagenet') img = image.load_img(IMG_PATH, target_size=(299, 299)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) preds = model.predict(x) #preds.tofile('out_0.txt', '\n') print('Keras Predicted:', decode_predictions(preds, top=5)[0]) model.save(KERAS_MODEL_PATH)
def get_model(self, freeze_base_model=False, freeze_initial_layers=0): # pretrained_model = Xception(weights="imagenet", include_top=False, input_shape=self.shape) input = Input(self.shape) pretrained_model = Xception(weights="imagenet", include_top=False) if freeze_base_model: pretrained_model.trainable = False else: for i, layer in enumerate(pretrained_model.layers): if i >= freeze_initial_layers: break layer.trainable = False outputs = pretrained_model(input, training=False) outputs = GlobalAveragePooling2D()(outputs) outputs = Dropout(0.25)(outputs) outputs = Dense(1024, activation='relu')(outputs) outputs = Dense(256, activation='relu')(outputs) outputs = Dense(64, activation='relu')(outputs) ppoi = Dense(1, activation='sigmoid', name='pe_present_on_image')(outputs) rlrg1 = Dense(1, activation='sigmoid', name='rv_lv_ratio_gte_1')(outputs) rlrl1 = Dense(1, activation='sigmoid', name='rv_lv_ratio_lt_1')(outputs) lspe = Dense(1, activation='sigmoid', name='leftsided_pe')(outputs) cpe = Dense(1, activation='sigmoid', name='chronic_pe')(outputs) rspe = Dense(1, activation='sigmoid', name='rightsided_pe')(outputs) aacpe = Dense(1, activation='sigmoid', name='acute_and_chronic_pe')(outputs) cnpe = Dense(1, activation='sigmoid', name='central_pe')(outputs) indt = Dense(1, activation='sigmoid', name='indeterminate')(outputs) self.model = Model(inputs=input, outputs={ 'pe_present_on_image': ppoi, 'rv_lv_ratio_gte_1': rlrg1, 'rv_lv_ratio_lt_1': rlrl1, 'leftsided_pe': lspe, 'chronic_pe': cpe, 'rightsided_pe': rspe, 'acute_and_chronic_pe': aacpe, 'central_pe': cnpe, 'indeterminate': indt }) self._compile_model() return self.model
def create_model(): # Image shape image_input = Input(shape=(160, 120, 3)) # Set weights to none. Remove dense layers of model model = Xception(input_tensor=image_input, include_top=False, weights=None) # Add custom fully connected layers last_layer = model.get_layer('block14_sepconv2_act').output x = GlobalAveragePooling2D()(last_layer) x = Dense(512, activation='relu', name='fc1')(x) x = Dense(128, activation='relu', name='fc2')(x) out = Dense(NUM_CLASSES, activation='linear', name='output')(x) return Model(image_input, out)
def predict_image(): #file = request.files['image'] #filename = os.path.join('selected', file.filename) #file.save(filename) # Load the VGG19 model # https://keras.io/applications/#VGG19 model = Xception(include_top=True, weights='imagenet') # Define default image size for VGG19 image_size = (299, 299) # initialize the response dictionary that will be returned message = request.get_json(force=True) encoded = message['image'] decoded = base64.b64decode(encoded) img = Image.open(io.BytesIO(decoded)) # if the image mode is not RGB, convert it if img.mode != "RGB": img = img.convert("RGB") # resize the input image and preprocess it img = img.resize(image_size) # Preprocess image for model prediction # This step handles scaling and normalization for VGG19 x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) # Make predictions predictions = model.predict(x) prediction = decode_predictions(predictions, top=1) print('Predicted:', decode_predictions(predictions, top=3)) response = { 'predictions': { 'label': np.array(prediction[0][0][1]).tolist(), 'probability': np.array(prediction[0][0][2]).tolist() } } print(response) # return the response dictionary as a JSON response return jsonify(response)
def train_xception(img_size=IMG_SIZE, batch_size=BATCH_SIZE): xception_model = Xception(weights='imagenet', include_top=False) xception_model.summary() x = layers.GlobalAveragePooling2D()(xception_model.output) x = layers.Flatten()(x) x = layers.Dense(512)(x) x = layers.Dense(512)(x) x = layers.Dense(1000)(x) model = tf.keras.Model(xception_model.input, x) model.summary() for layer in model.layers[:-3]: layer.trainable = False model = tf.keras.models.load_model('./Models/Xception') train_df = pd.read_csv('./classes_train.csv') validate_df = pd.read_csv('./classes_validate.csv') model.compile( loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True), optimizer=tf.keras.optimizers.Adam(beta_1=.999), metrics=['accuracy']) train_datagen = ImageDataGenerator(rescale=1. / 255) train_generator = train_datagen.flow_from_dataframe( dataframe=train_df, x_col='file', y_col='class_num', target_size=(img_size, img_size), validate_filenames=False, batch_size=batch_size, class_mode='categorical') validate_generator = train_datagen.flow_from_dataframe( dataframe=validate_df, x_col='file', y_col='class_num', validate_filenames=False, target_size=(img_size, img_size), batch_size=batch_size, class_mode='categorical') model.fit(train_generator, validation_data=validate_generator, epochs=10, workers=16, steps_per_epoch=VALID_SIZE // batch_size, callbacks=[ tf.keras.callbacks.ModelCheckpoint('./Models/Xception'), tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=2) ]) # print(model.evaluate(train_generator, steps=40, batch_size=batch_size*4)) model.save('./Models/Xception')