def transfer_model(model_name, input_shape, classes_nr): new_input = Input(shape=(input_shape[0], input_shape[1], 3)) if model_name == "vgg16": model = VGG16(include_top=False, input_tensor=new_input) if model_name == "densenet121": model = DenseNet121(include_top=False, input_tensor=new_input) if model_name == "inceptionv3": model = InceptionV3(include_top=False, input_tensor=new_input) if model_name == "mobilenet": model = MobileNet(include_top=False, input_tensor=new_input) if model_name == "resnet101": model = ResNet101(include_top=False, input_tensor=new_input) if model_name == "xception": model = Xception(include_top=False, input_tensor=new_input) for layer in model.layers: layer.trainable = False flat1 = layers.Flatten()(model.layers[-1].output) class1 = layers.Dense(1024, activation='relu')(flat1) drop1 = layers.Dropout(0.2)(class1) class2 = layers.Dense(256, activation='relu')(drop1) output = layers.Dense(classes_nr, activation='softmax')(class2) model = Model(inputs=model.inputs, outputs=output) return model
def __init__(self, data_format='channels_last'): """Constructor function.""" # determine the input shape if data_format == 'channels_last': input_shape = (IMAGE_SIZE[0], IMAGE_SIZE[1], 3) elif data_format == 'channels_first': input_shape = (3, IMAGE_SIZE[0], IMAGE_SIZE[1]) else: raise ValueError('unrecognized data format: ' + data_format) # build the Inception-v3 backbone & final classification layer K.set_image_data_format(data_format) import ssl ssl._create_default_https_context = ssl._create_unverified_context # net = InceptionV3() # print("==============",net) net = InceptionV3(include_top=False, weights=None, input_tensor=None, input_shape=input_shape, backend=K) x = net.output x = Conv2D(NUM_CLASSES, 8, data_format=data_format, activation='softmax', name='output')(x) x = Flatten(data_format=data_format)(x) net_final = Model(inputs=net.input, outputs=x) # obtain input & output tensors self.inputs = net_final.inputs self.outputs = net_final.outputs
def main(): from tensorflow.python.keras.models import Model from tensorflow.python.keras.layers import Flatten, Dense, Dropout, Conv2D from tensorflow.python.keras.applications.inception_v3 import InceptionV3, preprocess_input data_format = 'channels_last' input_shape = (299, 299, 3) if data_format == 'channels_last' else (3, 299, 299) #input_shape = (2048, 2048, 3) if data_format == 'channels_last' else (3, 2048, 2048) K.set_image_data_format(data_format) net = InceptionV3(include_top=False, weights=None, input_tensor=None, input_shape=input_shape) x = net.output x = Conv2D(2, 8, activation='softmax', name='output')(x) model = Model(inputs=net.input, outputs=x) model.load_weights(h5_path, by_name=True) converted_output_node_names = [node.op.name for node in model.outputs] print(('Converted output node names are: %s', str(converted_output_node_names))) sess = K.get_session() constant_graph = graph_util.convert_variables_to_constants( sess, sess.graph.as_graph_def(), converted_output_node_names) graph_io.write_graph(constant_graph, save_path, 'model.pb', as_text=False) sess.close()
def InceptionV3WithCustomLayers(nb_classes, input_shape, fc_size): """ Adding custom final layers on Inception_V3 model with imagenet weights Args: nb_classes: # of classes input_shape: input shape of the images Returns: new keras model with new added last layer/s and the base model which new layers are added """ base_model = InceptionV3(input_tensor=Input(shape=input_shape), weights='imagenet', include_top=False) x = base_model.output x = GlobalAveragePooling2D()(x) x = Dense(fc_size * 2, activation='relu')(x) # new FC layer, random init x = Dropout(0.3)(x) # x = Dense(FC_SIZE, activation='relu')(x) # new FC layer, random init # x = Dropout(0.5)(x) # x = Dense(FC_SIZE * 4, activation='relu')(x) # new FC layer, random init # x = Dropout(0.5)(x) predictions = Dense(nb_classes, activation='softmax')(x) # new softmax layer model = Model(outputs=predictions, inputs=base_model.input) return model, base_model
def load_model(model_arch): if model_arch == 'inceptionV3': model = InceptionV3(include_top=False, weights='imagenet', pooling='avg') height, width = 299, 299 else: raise NotImplementedError("That architecture is not implemented yet") return model, (height, width)
def create_model(num_hidden, num_classes): base_model = InceptionV3(include_top=False, weights='imagenet') x = base_model.output x = GlobalAveragePooling2D()(x) x = Dense(num_hidden, activation='relu')(x) predictions = Dense(num_classes, activation='softmax')(x) for layer in base_model.layers: layer.trainable = False model = Model(inputs=base_model.input, outputs=predictions) return model base_model = InceptionV3(include_top=False, weights='image') x = base_model.output x = GlobalAveragePooling2D()(x) x = Dense(num_hidden, activation='relu')(x) predictions = Dense(num_classes, activation='softmax')(x) for layer in base_model.layers: layer.trainable = False model = Model(inputs=base_model.input, outputs=predictions) return model
def get_model(): _model = InceptionV3(input_shape=(78, 78, 3), include_top=False, weights=None) _model.load_weights(weights_file) for layer in _model.layers: layer.trainable = False _model.summary() return _model
def setUp(self): self.model = InceptionV3(input_tensor=Input(shape=utils.INPUT_SHAPE), weights=None, include_top=True, classes=3) self.layers_bool = [] bool = True for layer in self.model.layers: layer.trainable = bool self.layers_bool.append(layer.trainable) # switch true and false every iteration bool = not bool
def classification( shape: Tuple[int, int, int] = (224, 224, 3), n_class: int = 3, model_net: str = "Resnet50V2", resnet_train: bool = True, ) -> Model: """ Modelo de classificação entre covid, normal e pneumonia Args: ----- input_size (tuple, optional): Tamanho da imagem de entrada. Defaults to (224, 224, 3). n_class (int, optional): Número de classes de saída. Defaults to 3. Returns: -------- (keras.Model) : Modelo do keras """ inputs = Input(shape, name="entrada_modelo") model = Conv2D( filters=3, kernel_size=(3, 3), padding="same", activation="relu", name="conv_gray_rgb", )(inputs) params = { "include_top": False, "weights": "imagenet", "input_shape": (shape[0], shape[1], 3), "pooling": "avg", } if model_net == "VGG19": base_model = VGG19(**params) elif model_net == "InceptionResNetV2": base_model = InceptionV3(**params) elif model_net == "MobileNetV2": base_model = MobileNetV3Small(**params) elif model_net == "DenseNet201": base_model = DenseNet201(**params) else: base_model = ResNet50V2(**params) base_model.trainable = resnet_train model = base_model(model) model = Dropout(0.5, name="drop_0")(model) model = Dense(units=256, name="dense_0")(model) model = Dropout(0.5, name="drop_1")(model) model = Dense(units=n_class, name="classifier")(model) predictions = Activation(activation="softmax", name="output")(model) return Model(inputs=inputs, outputs=predictions)
def define_model(IMAGE_DIMS, VEC_LEN, weight_dir): i10 = Input(shape=IMAGE_DIMS) i20 = Input(shape=IMAGE_DIMS) i30 = Input(shape=IMAGE_DIMS) t1 = Input(shape=(1, )) t2 = Input(shape=(1, )) print("[INFO] Weights restored from pre-trained InceptionV3!") encoder = InceptionV3(weights=weight_dir, include_top=False) pooling = GlobalAveragePooling2D() def l2_norm_(x): return K.sqrt(K.sum(K.square(x), 1)) def l2_normalize(x): return K.l2_normalize(x, 1) output = Dense(VEC_LEN, activation='sigmoid', name='encoder_output') o1 = encoder(i10) o2 = encoder(i20) o3 = encoder(i30) o1 = pooling(o1) # 全局平均池化层 o1 = output(o1) # 有1024个节点的全连接层 o2 = pooling(o2) # 全局平均池化层 o2 = output(o2) # 有1024个节点的全连接层 o3 = pooling(o3) # 全局平均池化层 o3 = output(o3) # 有1024个节点的全连接层 def distance(inputs): ap, an, margin, gthr = inputs ap_l2n = K.sqrt(K.sum(K.square(ap), axis=1, keepdims=True)) an_l2n = K.sqrt(K.sum(K.square(an), axis=1, keepdims=True)) d = K.minimum((an_l2n - ap_l2n), margin) g = K.maximum(ap_l2n, gthr) y = K.concatenate([d, g], axis=1) return y ap = Subtract()([o1, o2]) an = Subtract()([o1, o3]) val = Lambda(distance, name='margin')([ap, an, t1, t2]) model = Model(inputs=[i10, i20, i30, t1, t2], outputs=val) return model
def create_model(self): ''' InceptionV3を用いた機械学習モデルを構築します。 戻り値: compile済みモデルオブジェクト ''' # inputの定義:cam/img_array 形式 img_in = Input(shape=self.image_shape, name='img_in') # InceptionV3 canned model を使用 x = InceptionV3(weights='imagenet', include_top=False)(img_in) x = GlobalAveragePooling2D()(x) x = Dense(1024, activation='softmax')(x) x = Dropout(0.5)(x) # 従来モデルより流用 x = Flatten(name='flattened')(x) x = Dense(100, activation='relu')(x) x = Dropout(.1)(x) x = Dense(50, activation='relu')(x) x = Dropout(.1)(x) # outputs[0] の定義:15分類確率ベクトル angle_out = Dense(15, activation='softmax', name='angle_out')(x) # outputs[1] の定義:スロットルPWM値 throttle_out = Dense(1, activation='relu', name='throttle_out')(x) print('model throttle_out:' + str(throttle_out)) # モデルオブジェクトの定義 model = Model(inputs=[img_in], outputs=[angle_out, throttle_out]) model.compile(optimizer='adam', loss={ 'angle_out': 'categorical_crossentropy', 'throttle_out': 'mean_absolute_error' }, loss_weights={ 'angle_out': 0.9, 'throttle_out': .001 }) return model
def build_baseline_model(args, input_shape): """ Builds a baseline InceptionV3 model from tensorflow implementation with no trained weights loaded and including top layers for prediction Args: args: necessary args needed for training like train_data_dir, batch_size etc... input_shape: shape of input tensor Returns: baseline inceptionV3 model """ iv3 = InceptionV3(input_tensor=Input(shape=input_shape), weights=None, include_top=True, classes=args.nb_classes) iv3.compile(optimizer='RMSprop', loss='categorical_crossentropy', metrics=['accuracy']) return iv3
def get_model(self, model_name, weights): if model_name == 'InceptionV3': from tensorflow.python.keras.applications.inception_v3 import InceptionV3 base_model = InceptionV3(weights=weights, include_top=False) elif model_name == 'NASNetLarge': from tensorflow.python.keras.applications.nasnet import NASNetLarge base_model = NASNetLarge(weights=weights, include_top=False) elif model_name == 'DenseNet201': from tensorflow.python.keras.applications.densenet import DenseNet201 base_model = DenseNet201(weights=weights, include_top=False) elif model_name == 'Xception': from tensorflow.python.keras.applications.xception import Xception base_model = Xception(weights=weights, include_top=False) elif model_name == 'VGG16': from tensorflow.python.keras.applications.vgg16 import VGG16 base_model = VGG16(weights=weights, include_top=False) elif model_name == 'VGG19': from tensorflow.python.keras.applications.vgg19 import VGG19 base_model = VGG19(weights=weights, include_top=False) elif model_name == 'NASNetMobile': from tensorflow.python.keras.applications.nasnet import NASNetMobile base_model = NASNetMobile(weights=weights, include_top=False) elif model_name == 'MobileNetV2': from tensorflow.python.keras.applications.mobilenet_v2 import MobileNetV2 base_model = MobileNetV2(weights=weights, include_top=False) elif model_name == 'ResNet50': from tensorflow.python.keras.applications.resnet50 import ResNet50 base_model = ResNet50(weights=weights, include_top=False) elif model_name == 'InceptionResNetV2': from tensorflow.python.keras.applications.inception_resnet_v2 import InceptionResNetV2 base_model = InceptionResNetV2(weights=weights, include_top=False, ) else: raise KeyError('Unknown network.') x = base_model.output x = GlobalAveragePooling2D()(x) x = Dense(512, activation='relu')(x) predictions = Dense(1, activation='sigmoid')(x) model = Model(inputs=base_model.input, outputs=predictions) return model
def get_model(self, model_name, weights='imagenet'): if model_name == 'InceptionV3': from tensorflow.python.keras.applications.inception_v3 import InceptionV3 base_model = InceptionV3(weights=weights, include_top=False) elif model_name == 'NASNetLarge': from tensorflow.python.keras.applications.nasnet import NASNetLarge base_model = NASNetLarge(weights=weights, include_top=False) elif model_name == 'DenseNet201': from tensorflow.python.keras.applications.densenet import DenseNet201 base_model = DenseNet201(weights=weights, include_top=False) elif model_name == 'Xception': from tensorflow.python.keras.applications.xception import Xception base_model = Xception(weights=weights, include_top=False) elif model_name == 'VGG19': from tensorflow.python.keras.applications.vgg19 import VGG19 base_model = VGG19(weights=weights, include_top=False) elif model_name == 'NASNetMobile': from tensorflow.python.keras.applications.nasnet import NASNetMobile base_model = NASNetMobile(weights=weights, include_top=False) elif model_name == 'MobileNetV2': from tensorflow.python.keras.applications.mobilenet_v2 import MobileNetV2 base_model = MobileNetV2(weights=weights, include_top=False) elif model_name == 'ResNet50': from tensorflow.python.keras.applications.resnet50 import ResNet50 base_model = ResNet50(weights=weights, include_top=False) elif model_name == 'InceptionResNetV2': from tensorflow.python.keras.applications.inception_resnet_v2 import InceptionResNetV2 base_model = InceptionResNetV2(weights=weights, include_top=False) else: raise KeyError('Unknown network.') x = base_model.output x = GlobalAveragePooling2D()(x) # x = Dense(1024, activation='relu')(x) output = Dense(1, activation='sigmoid')(x) # output = SiameseLossLayer(self.batch_size, self.num_distort, self.num_level)(x) model = Model(inputs=base_model.input, outputs=output) self.name = model_name return model
def train(args): """Use transfer learning and fine-tuning to train a network on a new dataset""" nb_train_samples = get_nb_files(args.train_dir) nb_classes = len(glob.glob(args.train_dir + "/*")) nb_val_samples = get_nb_files(args.val_dir) nb_epoch = int(args.nb_epoch) batch_size = int(args.batch_size) # data prep train_datagen = ImageDataGenerator(preprocessing_function=preprocess_input, rotation_range=30, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.2, zoom_range=0.2, horizontal_flip=True) test_datagen = ImageDataGenerator(preprocessing_function=preprocess_input, rotation_range=30, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.2, zoom_range=0.2, horizontal_flip=True) train_generator = train_datagen.flow_from_directory( args.train_dir, target_size=(IM_WIDTH, IM_HEIGHT), batch_size=batch_size, ) validation_generator = test_datagen.flow_from_directory( args.val_dir, target_size=(IM_WIDTH, IM_HEIGHT), batch_size=batch_size, ) # setup model base_model = InceptionV3( weights='imagenet', include_top=False) #include_top=False excludes final FC layer model = add_new_last_layer(base_model, nb_classes) # transfer learning setup_to_transfer_learn(model, base_model) history_tl = model.fit_generator(train_generator, nb_epoch=nb_epoch, samples_per_epoch=nb_train_samples, validation_data=validation_generator, nb_val_samples=nb_val_samples, class_weight='auto') # fine-tuning setup_to_finetune(model) history_ft = model.fit_generator(train_generator, samples_per_epoch=nb_train_samples, nb_epoch=nb_epoch, validation_data=validation_generator, nb_val_samples=nb_val_samples, class_weight='auto') model.save(args.output_model_file) if args.plot: plot_training(history_ft)
def define_model(IMAGE_DIMS, VEC_LEN, weight_dir): i10 = Input(shape=IMAGE_DIMS) i20 = Input(shape=IMAGE_DIMS) i30 = Input(shape=IMAGE_DIMS) t1 = Input(shape=(1, )) t2 = Input(shape=(1, )) i1 = Lambda(lambda x: tf.div(tf.subtract(tf.cast(x, tf.float32), 127.5), 127.5))(i10) i2 = Lambda(lambda x: tf.div(tf.subtract(tf.cast(x, tf.float32), 127.5), 127.5))(i20) i3 = Lambda(lambda x: tf.div(tf.subtract(tf.cast(x, tf.float32), 127.5), 127.5))(i30) # i1 = tf.cast(i1,tf.float32) # i2 = tf.cast(i2, tf.float32) # i3 = tf.cast(i3, tf.float32) # # i1=tf.div(tf.subtract(i1,127.5),127.5) # i2 = tf.div(tf.subtract(i2, 127.5), 127.5) # i3 = tf.div(tf.subtract(i3, 127.5), 127.5) print("[INFO] Weights restored from pre-trained InceptionV3!") encoder = InceptionV3(weights=weight_dir, include_top=False) pooling = GlobalAveragePooling2D() def l2_normalize(x): return K.expand_dims(K.l2_normalize(x, 1)) val = Lambda(l2_normalize, name='margin')() BatchNormalization(axis=-1, momentum=0.99, epsilon=0.001, center=True, scale=True, beta_initializer='zeros', gamma_initializer='ones', moving_mean_initializer='zeros', moving_variance_initializer='ones', beta_regularizer=None, gamma_regularizer=None, beta_constraint=None, gamma_constraint=None) output = Dense(VEC_LEN, activation='tanh', name='encoder_output') o1 = encoder(i1) o2 = encoder(i2) o3 = encoder(i3) # o1 = i1 # o2 = i2 # o3 = i3 o1 = pooling(o1) # 全局平均池化层 # o1 = BatchNormalization(o1) o1 = output(o1) # 有1024个节点的全连接层 # o1 = NormLayer(o1) #o1 = Dropout(0.1)(o1) o2 = pooling(o2) # 全局平均池化层 # o2 = BatchNormalization(o2) o2 = output(o2) # 有1024个节点的全连接层 #o2 = Dropout(0.1)(o2) # o2 = NormLayer(o2) o3 = pooling(o3) # 全局平均池化层 # o3 = BatchNormalization(o3) o3 = output(o3) # 有1024个节点的全连接层 # o3 = NormLayer(o3) #o3 = Dropout(0.1)(o3) #print('[INFO] base_model_layers', len(encoder.layers)) def l2_normalize(x): return K.expand_dims(K.l2_normalize(x, 1)) def l2_norm(x): return K.sqrt(K.sum(K.square(x), 1)) def distance(inputs): ap, an, margin, gthr = inputs ap_l2n = K.sqrt(K.sum(K.square(ap), axis=1, keepdims=True)) an_l2n = K.sqrt(K.sum(K.square(an), axis=1, keepdims=True)) d = K.minimum((an_l2n - ap_l2n), margin) # d=an_l2n # g=ap_l2n g = K.maximum(ap_l2n, gthr) y = K.concatenate([d, g], axis=1) return y ap = Subtract()([o1, o2]) an = Subtract()([o1, o3]) val = Lambda(distance, name='margin')([ap, an, t1, t2]) # val = Concatenate()([d, g]) model = Model(inputs=[i10, i20, i30, t1, t2], outputs=val) # K.clear_session() return model
def initModel(name, input_shape): model = Sequential() if name == 'test': model.add(Dense(512, input_shape=input_shape)) model.add(Activation("relu")) model.add(Dense(7)) model.add(Activation("sigmoid")) elif name == 'alexnet': # 1st Convolutional Layer model.add( Conv2D(filters=96, input_shape=input_shape, kernel_size=(11, 11), strides=(4, 4), padding='valid')) model.add(Activation('relu')) # Max Pooling model.add( MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='valid')) # 2nd Convolutional Layer model.add( Conv2D(filters=256, kernel_size=(11, 11), strides=(1, 1), padding='valid')) model.add(Activation('relu')) # Max Pooling model.add( MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='valid')) # 3rd Convolutional Layer model.add( Conv2D(filters=384, kernel_size=(3, 3), strides=(1, 1), padding='valid')) model.add(Activation('relu')) # 4th Convolutional Layer model.add( Conv2D(filters=384, kernel_size=(3, 3), strides=(1, 1), padding='valid')) model.add(Activation('relu')) # 5th Convolutional Layer model.add( Conv2D(filters=256, kernel_size=(3, 3), strides=(1, 1), padding='valid')) model.add(Activation('relu')) # Max Pooling model.add( MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='valid')) # Passing it to a Fully Connected layer model.add(Flatten()) # 1st Fully Connected Layer model.add(Dense(4096, input_shape=input_shape)) model.add(Activation('relu')) # Add Dropout to prevent overfitting model.add(Dropout(0.4)) # 2nd Fully Connected Layer model.add(Dense(4096)) model.add(Activation('relu')) # Add Dropout model.add(Dropout(0.4)) # 3rd Fully Connected Layer model.add(Dense(1000)) model.add(Activation('relu')) # Add Dropout model.add(Dropout(0.4)) # Output Layer model.add(Dense(7)) model.add(Activation('softmax')) elif name == 'VGG': model.add( Conv2D(64, (3, 3), input_shape=input_shape, padding='same', activation='relu')) model.add(Conv2D(64, (3, 3), activation='relu', padding='same')) model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) model.add(Conv2D(128, (3, 3), activation='relu', padding='same')) model.add(Conv2D(128, (3, 3), activation='relu', padding='same')) model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) model.add(Conv2D(256, (3, 3), activation='relu', padding='same')) model.add(Conv2D(256, (3, 3), activation='relu', padding='same')) model.add(Conv2D(256, (3, 3), activation='relu', padding='same')) model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) model.add(Conv2D(512, (3, 3), activation='relu', padding='same')) model.add(Conv2D(512, (3, 3), activation='relu', padding='same')) model.add(Conv2D(512, (3, 3), activation='relu', padding='same')) model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) model.add(Conv2D(512, (3, 3), activation='relu', padding='same')) model.add(Conv2D(512, (3, 3), activation='relu', padding='same')) model.add(Conv2D(512, (3, 3), activation='relu', padding='same')) model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) model.add(Flatten()) model.add(Dense(4096, input_shape=input_shape, activation='relu')) model.add(Dense(4096, activation='relu')) model.add(Dense(7, activation='softmax')) elif name == 'Squeezenet': img_input = Input(shape=input_shape) x = Conv2D(64, (3, 3), strides=(2, 2), padding='valid', name='conv1')(img_input) x = Activation('relu', name='relu_conv1')(x) x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='pool1')(x) x = fire_module(x, fire_id=2, squeeze=16, expand=64) x = fire_module(x, fire_id=3, squeeze=16, expand=64) x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='pool3')(x) x = fire_module(x, fire_id=4, squeeze=32, expand=128) x = fire_module(x, fire_id=5, squeeze=32, expand=128) x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='pool5')(x) x = fire_module(x, fire_id=6, squeeze=48, expand=192) x = fire_module(x, fire_id=7, squeeze=48, expand=192) x = fire_module(x, fire_id=8, squeeze=64, expand=256) x = fire_module(x, fire_id=9, squeeze=64, expand=256) x = Dropout(0.5, name='drop9')(x) x = Conv2D(7, (1, 1), padding='valid', name='conv10')(x) #uses classes bias_initializer=class_bias x = Activation('relu', name='relu_conv10')(x) x = GlobalAveragePooling2D()(x) x = Activation('softmax', name='loss')(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. inputs = img_input model = Model(inputs, x, name='squeezenet') elif name == 'deepsqueeze': img_input = Input(shape=input_shape) x = Conv2D(64, (3, 3), strides=(2, 2), padding='valid', name='conv1')(img_input) x = Activation('relu', name='relu_conv1')(x) x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='pool1')(x) x = fire_module(x, fire_id=2, squeeze=16, expand=64) x = fire_module(x, fire_id=3, squeeze=16, expand=64) x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='pool3')(x) x = fire_module(x, fire_id=4, squeeze=32, expand=128) x = fire_module(x, fire_id=5, squeeze=32, expand=128) x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='pool5')(x) x = fire_module(x, fire_id=6, squeeze=32, expand=128) x = fire_module(x, fire_id=7, squeeze=32, expand=128) x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='pool7')(x) x = fire_module(x, fire_id=8, squeeze=32, expand=128) x = fire_module(x, fire_id=9, squeeze=32, expand=128) x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='pool9')(x) x = fire_module(x, fire_id=14, squeeze=32, expand=128) x = fire_module(x, fire_id=15, squeeze=32, expand=128) x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='pool11')(x) x = fire_module(x, fire_id=10, squeeze=48, expand=192) x = fire_module(x, fire_id=11, squeeze=48, expand=192) x = fire_module(x, fire_id=12, squeeze=64, expand=256) x = fire_module(x, fire_id=13, squeeze=64, expand=256) x = Dropout(0.5, name='drop13')(x) x = Conv2D(7, (1, 1), padding='valid', name='conv10')(x) # uses classes x = Activation('relu', name='relu_conv10')(x) # x = GlobalAveragePooling2D()(x) x = GlobalAveragePooling2D(data_format='channels_last')(x) x = Activation('softmax', name='loss')(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. inputs = img_input model = Model(inputs, x, name='deepsqueeze') elif name == 'son_of_deepsqueeze': img_input = Input(shape=input_shape) x = Conv2D(64, (3, 3), strides=(2, 2), padding='valid', name='conv1')(img_input) x = Activation('relu', name='relu_conv1')(x) x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='pool1')(x) x = fire_module(x, fire_id=2, squeeze=16, expand=64) x = fire_module(x, fire_id=3, squeeze=16, expand=64) x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='pool3')(x) x = fire_module(x, fire_id=4, squeeze=32, expand=128) x = fire_module(x, fire_id=5, squeeze=32, expand=128) x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='pool5')(x) x = fire_module(x, fire_id=6, squeeze=48, expand=192) x = fire_module(x, fire_id=7, squeeze=48, expand=192) x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='pool7')(x) x = fire_module(x, fire_id=8, squeeze=64, expand=256) x = fire_module(x, fire_id=9, squeeze=64, expand=256) x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='pool9')(x) x = fire_module(x, fire_id=14, squeeze=80, expand=320) x = fire_module(x, fire_id=15, squeeze=80, expand=320) x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='pool11')(x) x = fire_module(x, fire_id=10, squeeze=96, expand=384) x = fire_module(x, fire_id=11, squeeze=96, expand=384) x = fire_module(x, fire_id=12, squeeze=112, expand=448) x = fire_module(x, fire_id=13, squeeze=112, expand=448) x = Dropout(0.5, name='drop13')(x) x = Conv2D(7, (1, 1), padding='valid', name='conv10')(x) # uses classes x = Activation('relu', name='relu_conv10')(x) # x = GlobalAveragePooling2D()(x) x = GlobalAveragePooling2D(data_format='channels_last')(x) x = Activation('softmax', name='loss')(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. inputs = img_input model = Model(inputs, x, name='son_of_deepsqueeze') elif name == 'the_big_squeeze': #snow_squeeze img_input = Input(shape=input_shape) x = Conv2D(64, (3, 3), strides=(2, 2), padding='valid', name='conv1')(img_input) x = Activation('relu', name='relu_conv1')(x) x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='pool1')(x) x = fire_module(x, fire_id=2, squeeze=16, expand=64) x = fire_module(x, fire_id=3, squeeze=16, expand=64) x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='pool3')(x) x = fire_module(x, fire_id=4, squeeze=32, expand=128) x = fire_module(x, fire_id=5, squeeze=32, expand=128) x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='pool5')(x) x = fire_module(x, fire_id=6, squeeze=48, expand=192) x = fire_module(x, fire_id=7, squeeze=48, expand=192) x = fire_module(x, fire_id=8, squeeze=64, expand=256) x = fire_module(x, fire_id=9, squeeze=64, expand=256) x = Dropout(0.5, name='drop9')(x) x = Flatten()(x) x = Dense(7, activation='softmax')(x) # Ensure that the model takes into account # any potential predecessors of `input_tensor`. inputs = img_input model = Model(inputs, x, name='the_big_squeeze') elif name == 'inceptionV3': InceptionV3(include_top=False, input_shape=input_shape, classes=7) return model
from networks.UNet import UNet from networks.VggNet import Vgg19Net, SmallVggNet from training_loops.OptimizerHelper import VggOneBlockFunctional model = Vgg19Net.build(IMAGE_DIMS[0], IMAGE_DIMS[1], IMAGE_DIMS[2], classes=3) print('VGG19Net') model.summary() model = SmallVggNet.build(IMAGE_DIMS[0], IMAGE_DIMS[1], IMAGE_DIMS[2], classes=3) print('SmallVggNet') model.summary() model = InceptionV3(input_shape=IMAGE_DIMS, classes=3, weights=None) print('InceptionV3') model.summary() model = UNet.build([IMAGE_DIMS[0], IMAGE_DIMS[1], 1], 3) print('U-Net') model.summary() model = resnet50(IMAGE_DIMS, 3) print('ResNet50') model.summary() model = VggOneBlockFunctional.build(IMAGE_DIMS[0], IMAGE_DIMS[1], IMAGE_DIMS[2], classes=3)
def process_images_thru_tl(batch_size=32, input1=1024, input2=1024, model_name="vgg"): with tf.device("/device:GPU:1"): if model_name == "vgg": model = VGG16(weights = "imagenet", include_top=False, input_shape = [224, 224, 3]) size = 224 elif model_name == "inceptionv3": model = InceptionV3(weights = "imagenet", include_top=False, input_shape = [299, 299, 3]) size = 299 elif model_name == "resnet50": model = ResNet50(weights = "imagenet", include_top=False, input_shape = [224, 224, 3]) size = 224 elif model_name == "mobilenet": model = ResNet50(weights = "imagenet", include_top=False, input_shape = [224, 224, 3]) size = 224 elif model_name == "xception": model = Xception(weights = "imagenet", include_top=False) size = 299 print("%s %d %d %d" % (model_name, input1, input2, batch_size)) model.summary() model.get_weights() labels = [] batch = [] # input_ = Input(shape=(size,size,3),name = 'image_input') output_ = model.output with tf.device("/device:GPU:1"): if model_name == "inceptionv3" or model_name == "xception": x = GlobalAveragePooling2D(name='avg_pool')(output_) else: x = Flatten(name='flatten')(output_) if input1 != 0: x = Dense(input1, activation='relu', name='fc1')(x) if input2 != 0: x = Dense(input2, activation='relu', name='fc2')(x) x = Dense(128, activation='softmax', name='predictions')(x) for layer in model.layers: layer.trainable = False my_model = Model(inputs=output_, outputs=x) my_model.summary() if os.path.exists("weights_%s_%d_%d_%d.h5" % (model_name, input1, input2, batch_size)): my_model.load_weights("weights_%s_%d_%d_%d.h5" % (model_name, input1, input2, batch_size)) my_model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) train_generator = get_train_data.train_generator(img_size=size, batch_size=batch_size) valid_generator = get_train_data.valid_generator(img_size=size, batch_size=8) csv_logger = CSVLogger('log.csv', append=True, separator=',') my_model.fit_generator( train_generator, steps_per_epoch=2000,#1000 epochs=10, validation_data=valid_generator, validation_steps=200,#200 callbacks=[csv_logger]) my_model.save_weights("weights_%s_%d_%d_%d.h5" % (model_name, input1, input2, batch_size))
def fit(self, learning_rate=1e-4, epochs=5, activation='relu', dropout=0, hidden_size=1024, nb_layers=1, include_class_weight=False, batch_size=20, save_model=False, verbose=True, fine_tuning=False, NB_IV3_LAYERS_TO_FREEZE=279, use_TPU=False, transfer_model='Inception', min_accuracy=None, extract_SavedModel=False): if transfer_model in ['Inception', 'Xception', 'Inception_Resnet']: target_size = (299, 299) else: target_size = (224, 224) #We expect the classes to be the name of the folders in the training set self.categories = os.listdir(TRAIN_DIR) """ helper functions to to build tensors inspired by https://www.tensorflow.org/tutorials/load_data/images """ def prepare_image(img_path): #reshape the image image = Image.open(img_path) image = image.resize(target_size, PIL.Image.BILINEAR).convert("RGB") #convert the image into a numpy array, and expend to a size 4 tensor image = img_to_array(image) #rescale the pixels to a 0-1 range image = image.astype(np.float32) / 255 return image def generate_tuples(img_folder): #loop through all the images # Get all file names of images present in folder classes = os.listdir(img_folder) classes_paths = [ os.path.abspath(os.path.join(img_folder, i)) for i in classes ] x = [] y = [] for i, j in enumerate(classes): #for all the classes, get the list of pictures img_paths = os.listdir(classes_paths[i]) img_paths = [ os.path.abspath(os.path.join(classes_paths[i], x)) for x in img_paths ] for img_path in img_paths: x.append(prepare_image(img_path)) y = y + [i] return (np.array(x), np.array(y).astype(np.int32)) #get training data (x_train, y_train) = generate_tuples(parentdir + '/data/image_dataset/train') (x_val, y_val) = generate_tuples(parentdir + '/data/image_dataset/val') #train input_function: see https://colab.research.google.com/drive/1F8txK1JLXKtAkcvSRQz2o7NSTNoksuU2#scrollTo=abbwQQfH0td3 def get_training_dataset(batch_size=batch_size): # Convert the inputs to a Dataset. dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train)) # Shuffle, repeat, and batch the examples. dataset = dataset.shuffle(1000).repeat().batch(batch_size, drop_remainder=True) return dataset def get_validation_dataset(batch_size=batch_size): # Convert the inputs to a Dataset. dataset = tf.data.Dataset.from_tensor_slices((x_val, y_val)) # Shuffle, repeat, and batch the examples. dataset = dataset.shuffle(1000).repeat().batch(batch_size, drop_remainder=True) return dataset #if we want stop training when no sufficient improvement in accuracy has been achieved if min_accuracy is not None: callback = EarlyStopping(monitor='acc', baseline=min_accuracy) callback = [callback] else: callback = None #load the pretrained model, without the classification (top) layers if transfer_model == 'Xception': base_model = Xception(weights='imagenet', include_top=False, input_shape=(299, 299, 3)) elif transfer_model == 'Inception_Resnet': base_model = InceptionResNetV2(weights='imagenet', include_top=False, input_shape=(299, 299, 3)) elif transfer_model == 'Resnet': base_model = ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3)) else: base_model = InceptionV3(weights='imagenet', include_top=False, input_shape=(299, 299, 3)) #Add the classification layers using Keras functional API x = base_model.output x = GlobalAveragePooling2D()(x) for _ in range(nb_layers): x = Dense(hidden_size, activation=activation)( x) #Hidden layer for classification if dropout > 0: x = Dropout(rate=dropout)(x) predictions = Dense(len(self.categories), activation='softmax')(x) #Output layer model = Model(inputs=base_model.input, outputs=predictions) #Set only the top layers as trainable (if we want to do fine-tuning, #we can train the base layers as a second step) for layer in base_model.layers: layer.trainable = False #Define the optimizer and the loss, and compile the model loss = 'sparse_categorical_crossentropy' if use_TPU: #if we want to try out the TPU, it looks like we currently need to use #tensorflow optimizers...see https://stackoverflow.com/questions/52940552/valueerror-operation-utpu-140462710602256-varisinitializedop-has-been-marked #...and https://www.youtube.com/watch?v=jgNwywYcH4w optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) model.compile(optimizer=optimizer, loss=sparse_softmax_cross_entropy, metrics=['acc']) TPU_WORKER = 'grpc://' + os.environ['COLAB_TPU_ADDR'] model = tf.contrib.tpu.keras_to_tpu_model( model, strategy=tf.contrib.tpu.TPUDistributionStrategy( tf.contrib.cluster_resolver.TPUClusterResolver( TPU_WORKER))) tf.logging.set_verbosity(tf.logging.INFO) else: optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) model.compile(optimizer=optimizer, loss=loss, metrics=['acc']) #if we want to weight the classes given the imbalanced number of images if include_class_weight: from sklearn.utils.class_weight import compute_class_weight cls_train = self.categories class_weight = compute_class_weight(class_weight='balanced', classes=np.unique(cls_train), y=cls_train) else: class_weight = None steps_per_epoch = int( sum([ len(files) for r, d, files in os.walk(parentdir + '/data/image_dataset/train') ]) / batch_size) validation_steps = int( sum([ len(files) for r, d, files in os.walk(parentdir + '/data/image_dataset/val') ]) / batch_size) #Fit the model if use_TPU: history = model.fit(get_training_dataset, steps_per_epoch=steps_per_epoch, epochs=epochs, validation_data=get_validation_dataset, validation_steps=validation_steps, verbose=verbose, callbacks=callback, class_weight=class_weight) else: history = model.fit(get_training_dataset(), steps_per_epoch=steps_per_epoch, epochs=epochs, validation_data=get_validation_dataset(), validation_steps=validation_steps, verbose=verbose, callbacks=callback, class_weight=class_weight) #Fine-tune the model, if we wish so if fine_tuning and not model.stop_training: print('============') print('Begin fine-tuning') print('============') #declare the first layers as trainable for layer in model.layers[:NB_IV3_LAYERS_TO_FREEZE]: layer.trainable = False for layer in model.layers[NB_IV3_LAYERS_TO_FREEZE:]: layer.trainable = True model.compile(optimizer=tf.train.AdamOptimizer( learning_rate=learning_rate * 0.1), loss=loss, metrics=['acc']) #Fit the model if use_TPU: history = model.fit(get_training_dataset, steps_per_epoch=steps_per_epoch, epochs=epochs, validation_data=get_validation_dataset, validation_steps=validation_steps, verbose=verbose, callbacks=callback, class_weight=class_weight) else: history = model.fit(get_training_dataset(), steps_per_epoch=steps_per_epoch, epochs=epochs, validation_data=get_validation_dataset(), validation_steps=validation_steps, verbose=verbose, callbacks=callback, class_weight=class_weight) #Evaluate the model, just to be sure self.fitness = history.history['val_categorical_accuracy'][-1] #Save the model if save_model: if not os.path.exists(parentdir + '/data/trained_models'): os.makedirs(parentdir + '/data/trained_models') model.save(parentdir + '/data/trained_models/trained_model.h5') print('Model saved!') #save model in production format if extract_SavedModel: export_path = "./image_classifier/1/" with K.get_session() as sess: tf.saved_model.simple_save( sess, export_path, inputs={'input_image': model.input}, outputs={t.name: t for t in model.outputs}) else: self.model = model del history del model
valid_datagen = ImageDataGenerator() valid_batches = valid_datagen.flow_from_directory(DATATEST_PATH, target_size=IMAGE_SIZE, interpolation='bicubic', class_mode='categorical', shuffle=False, batch_size=BATCH_SIZE) # 輸出各類別的索引值 for cls, idx in train_batches.class_indices.items(): print('Class #{} = {}'.format(idx, cls)) # 以訓練好的 ResNet50 為基礎來建立模型, # 捨棄 ResNet50 頂層的 fully connected layers net = InceptionV3(include_top=False, weights='imagenet', input_tensor=None, input_shape=(IMAGE_SIZE[0], IMAGE_SIZE[1], 3)) x = net.output x = Flatten()(x) # 增加 DropOut layer x = Dropout(0.5)(x) # 增加 Dense layer,以 softmax 產生個類別的機率值 output_layer = Dense(NUM_CLASSES, activation='softmax', name='softmax')(x) # 設定凍結與要進行訓練的網路層 net_final = Model(inputs=net.input, outputs=output_layer) for layer in net_final.layers[:FREEZE_LAYERS]: layer.trainable = False for layer in net_final.layers[FREEZE_LAYERS:]:
img_width, img_height = 299, 299 epochs = 1 # Data Augmentation train_datagen = ImageDataGenerator(rescale=1. / 255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True) train_generator = train_datagen.flow_from_directory( directory=train_data_dir, target_size=[img_width, img_height], class_mode='categorical') # Step 2-1: Replace softmax Layer and add one dense layer base_model = InceptionV3(weights='imagenet', include_top=False) x = base_model.output x = GlobalAveragePooling2D()(x) x = Dense(1024, activation='relu')(x) prediction = Dense(2, activation='softmax')(x) model = Model(inputs=base_model.input, outputs=prediction) for layer in model.layers: layer.trainable = False model.compile(loss='binary_crossentropy', optimizer=optimizers.SGD(lr=1e-4, momentum=0.9), metrics=['accuracy']) model.fit_generator(train_generator,steps_per_epoch=10,epochs=epochs) # Step 2-2: Unfreeze and train the top 2 layers for layer in model.layers[:172]:
def predict_inceptionv3(): # InceptionV3 generators test_batches = ImageDataGenerator( preprocessing_function= \ applications.inception_v3.preprocess_input).flow_from_directory( test2_path, target_size=(299, 299), batch_size=test_batch_size, shuffle=False) input_tensor = Input(shape=(299, 299, 3)) #Loading the model model = InceptionV3(input_tensor=input_tensor, weights='imagenet', include_top=False) # add a global spatial average pooling layer x = model.output x = GlobalAveragePooling2D()(x) x = Dense(1024, activation='relu')(x) predictions = Dense(7, activation='softmax')(x) # this is the model we will train model = Model(inputs=model.input, outputs=predictions) model.load_weights('modelInceptionV3.h5') test_batches.reset() test_labels = test_batches.classes # Make predictions predictions = model.predict_generator(test_batches, steps=test_steps, verbose=1) # Declare a function for plotting the confusion matrix def plot_confusion_matrix(cm, classes, normalize=False, title='Confusion matrix', cmap=plt.cm.Blues): """ This function prints and plots the confusion matrix. Normalization can be applied by setting `normalize=True`. """ if normalize: cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis] print("Normalized confusion matrix") else: print('Confusion matrix, without normalization') print(cm) plt.imshow(cm, interpolation='nearest', cmap=cmap) plt.title(title) plt.colorbar() tick_marks = np.arange(len(classes)) plt.xticks(tick_marks, classes, rotation=45) plt.yticks(tick_marks, classes) fmt = '.2f' if normalize else 'd' thresh = cm.max() / 2. for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])): plt.text(j, i, format(cm[i, j], fmt), horizontalalignment="center", color="white" if cm[i, j] > thresh else "black") plt.ylabel('True label') plt.xlabel('Predicted label') plt.tight_layout() plt.savefig('confusion_matrix_InceptionV3.png') cm = confusion_matrix(test_labels, predictions.argmax(axis=1)) cm_plot_labels = ['akiec', 'bcc', 'bkl', 'df', 'mel', 'nv', 'vasc'] plot_confusion_matrix(cm, cm_plot_labels) recall = np.diag(cm) / np.sum(cm, axis=1) precision = np.diag(cm) / np.sum(cm, axis=0) print("Mean recall " + str(np.mean(recall).item())) print("Mean precision " + str(np.mean(precision).item())) print("Mean recall " + str( recall_score( test_labels, predictions.argmax(axis=1), average='weighted'))) print("Balanced Accuracy " + str(balanced_accuracy_score(test_labels, predictions.argmax( axis=1)))) print("Mean Precision " + str( precision_score( test_labels, predictions.argmax(axis=1), average='weighted'))) print("Mean f1 score " + str( f1_score(test_labels, predictions.argmax(axis=1), average='weighted'))) file = open("InceptionV3_results.txt", "w+") file.write("Mean recall " + str(np.mean(recall).item()) + "\n") file.write("Mean precision " + str(np.mean(precision).item()) + "\n") file.write("Mean recall " + str( recall_score( test_labels, predictions.argmax(axis=1), average='weighted')) + "\n") file.write( "Balanced Accuracy " + str(balanced_accuracy_score(test_labels, predictions.argmax(axis=1))) + "\n") file.write("Mean Precision " + str( precision_score( test_labels, predictions.argmax(axis=1), average='weighted')) + "\n") file.write("Mean f1 score " + str( f1_score(test_labels, predictions.argmax( axis=1), average='weighted')) + "\n") file.close() predicted_class_indices = np.argmax(predictions, axis=1) labels = train_batches.class_indices labels = dict((v, k) for k, v in labels.items()) predictions = [labels[k] for k in predicted_class_indices] filenames = test_batches.filenames results = pd.DataFrame({ "Filename": filenames, "InceptionV3_Predictions": predictions }) results.to_csv("test_prediction_inceptionv3.csv", index=False)
log_dir, checkpoints = keras_helper.ask_what_to_do_with_logfiles( log_dir='./logs/inception_v3/', ckpt="checkpoints/inception_v3/{epoch:02d}.ckpt") dg = keras_helper.DataGenerators( train_path="../../Dataset/sample_sizes/64_images/", test_path="../../Dataset/test/", enable_augmentation=True, preprocessing_fn=preprocess_input) train_generator, test_generator, tensorboard_generator = dg.create_data_generators( 299) train_samples, test_samples = dg.get_samples() batch_size = dg.get_batch_size() model = keras_helper.create_model(InceptionV3(input_shape=(299, 299, 3), include_top=False), train_up_to=20) model.compile(Adam(lr=0.0001), loss="categorical_crossentropy", metrics=["accuracy", keras_helper.top_3_acc]) print(model.summary()) tensorboard_callback = keras_callbacks.TensorBoardImage(log_dir=log_dir, model=model, DataGenerator=dg) checkpoint_callback = tf.keras.callbacks.ModelCheckpoint(checkpoints, period=20) model.fit_generator(train_generator, steps_per_epoch=train_samples // batch_size,
def fit(self, learning_rate=1e-4, epochs=5, activation='relu', dropout=0, hidden_size=1024, nb_layers=1, include_class_weight=False, batch_size=20, save_model=False, verbose=True, fine_tuning=False, NB_IV3_LAYERS_TO_FREEZE=279, use_TPU=False, transfer_model='Inception', min_accuracy=None, extract_SavedModel=False): #read the tfrecords data TRAIN_DATA = tf.data.TFRecordDataset(['train.tfrecord']) VAL_DATA = tf.data.TFRecordDataset(['val.tfrecord']) print('Read the TFrecords') if transfer_model in ['Inception', 'Xception', 'Inception_Resnet']: target_size = (299, 299) else: target_size = (224, 224) #We expect the classes to be the name of the folders in the training set self.categories = os.listdir(TRAIN_DIR) """ helper functions to load tfrecords. Strongly inspired by https://colab.research.google.com/github/GoogleCloudPlatform/training-data-analyst/blob/master/courses/fast-and-lean-data-science/07_Keras_Flowers_TPU_playground.ipynb#scrollTo=LtAVr-4CP1rp """ def read_tfrecord(example): features = { "image": tf.FixedLenFeature( (), tf.string), # tf.string means byte string "label": tf.FixedLenFeature((), tf.int64) } example = tf.parse_single_example(example, features) image = tf.image.decode_jpeg(example['image']) image = tf.cast( image, tf.float32) / 255.0 # convert image to floats in [0, 1] range image = tf.image.resize_images( image, size=[*target_size], method=tf.image.ResizeMethod.BILINEAR) feature = tf.reshape(image, [*target_size, 3]) label = tf.cast(example['label'], tf.int32) # byte string target = tf.one_hot(label, len(self.categories)) return feature, target def get_training_dataset(): dataset = TRAIN_DATA.map(read_tfrecord) dataset = dataset.cache() dataset = dataset.repeat() dataset = dataset.shuffle(1000) dataset = dataset.batch( batch_size, drop_remainder=True) # drop_remainder needed on TPU dataset = dataset.prefetch( -1 ) # prefetch next batch while training (-1: autotune prefetch buffer size) return dataset def get_validation_dataset(): dataset = VAL_DATA.map(read_tfrecord) dataset = dataset.cache() dataset = dataset.repeat() dataset = dataset.shuffle(1000) dataset = dataset.batch( batch_size, drop_remainder=True) # drop_remainder needed on TPU dataset = dataset.prefetch( -1 ) # prefetch next batch while training (-1: autotune prefetch buffer size) return dataset #if we want stop training when no sufficient improvement in accuracy has been achieved if min_accuracy is not None: callback = EarlyStopping(monitor='categorical_accuracy', baseline=min_accuracy) callback = [callback] else: callback = None #load the pretrained model, without the classification (top) layers if transfer_model == 'Xception': base_model = Xception(weights='imagenet', include_top=False, input_shape=(299, 299, 3)) elif transfer_model == 'Inception_Resnet': base_model = InceptionResNetV2(weights='imagenet', include_top=False, input_shape=(299, 299, 3)) elif transfer_model == 'Resnet': base_model = ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3)) else: base_model = InceptionV3(weights='imagenet', include_top=False, input_shape=(299, 299, 3)) #Add the classification layers using Keras functional API x = base_model.output x = GlobalAveragePooling2D()(x) for _ in range(nb_layers): x = Dense(hidden_size, activation=activation)( x) #Hidden layer for classification if dropout > 0: x = Dropout(rate=dropout)(x) predictions = Dense(len(self.categories), activation='softmax')(x) #Output layer model = Model(inputs=base_model.input, outputs=predictions) #Set only the top layers as trainable (if we want to do fine-tuning, #we can train the base layers as a second step) for layer in base_model.layers: layer.trainable = False #Define the optimizer and the loss, and compile the model loss = 'categorical_crossentropy' if use_TPU: #if we want to try out the TPU, it looks like we currently need to use #tensorflow optimizers...see https://stackoverflow.com/questions/52940552/valueerror-operation-utpu-140462710602256-varisinitializedop-has-been-marked #...and https://www.youtube.com/watch?v=jgNwywYcH4w optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate) tpu_optimizer = tf.contrib.tpu.CrossShardOptimizer(optimizer) model.compile(optimizer=tpu_optimizer, loss=loss, metrics=['categorical_accuracy']) TPU_WORKER = 'grpc://' + os.environ['COLAB_TPU_ADDR'] model = tf.contrib.tpu.keras_to_tpu_model( model, strategy=tf.contrib.tpu.TPUDistributionStrategy( tf.contrib.cluster_resolver.TPUClusterResolver( TPU_WORKER))) tf.logging.set_verbosity(tf.logging.INFO) else: optimizer = Adam(lr=learning_rate) model.compile(optimizer=optimizer, loss=loss, metrics=['categorical_accuracy']) #if we want to weight the classes given the imbalanced number of images if include_class_weight: from sklearn.utils.class_weight import compute_class_weight cls_train = self.categories class_weight = compute_class_weight(class_weight='balanced', classes=np.unique(cls_train), y=cls_train) else: class_weight = None steps_per_epoch = int( sum([ len(files) for r, d, files in os.walk(parentdir + '/data/image_dataset/train') ]) / batch_size) validation_steps = int( sum([ len(files) for r, d, files in os.walk(parentdir + '/data/image_dataset/val') ]) / batch_size) #Fit the model if use_TPU: history = model.fit(get_training_dataset, steps_per_epoch=steps_per_epoch, epochs=epochs, validation_data=get_validation_dataset, validation_steps=validation_steps, verbose=verbose, callbacks=callback, class_weight=class_weight) else: history = model.fit(get_training_dataset(), steps_per_epoch=steps_per_epoch, epochs=epochs, validation_data=get_validation_dataset(), validation_steps=validation_steps, verbose=verbose, callbacks=callback, class_weight=class_weight) #Fine-tune the model, if we wish so if fine_tuning and not model.stop_training: print('============') print('Begin fine-tuning') print('============') #declare the first layers as trainable for layer in model.layers[:NB_IV3_LAYERS_TO_FREEZE]: layer.trainable = False for layer in model.layers[NB_IV3_LAYERS_TO_FREEZE:]: layer.trainable = True model.compile(optimizer=Adam(lr=learning_rate * 0.1), loss=loss, metrics=['categorical_accuracy']) #Fit the model if use_TPU: history = model.fit(get_training_dataset, steps_per_epoch=steps_per_epoch, epochs=epochs, validation_data=get_validation_dataset, validation_steps=validation_steps, verbose=verbose, callbacks=callback, class_weight=class_weight) else: history = model.fit(get_training_dataset(), steps_per_epoch=steps_per_epoch, epochs=epochs, validation_data=get_validation_dataset(), validation_steps=validation_steps, verbose=verbose, callbacks=callback, class_weight=class_weight) #Evaluate the model, just to be sure self.fitness = history.history['val_categorical_accuracy'][-1] #Save the model if save_model: if not os.path.exists(parentdir + '/data/trained_models'): os.makedirs(parentdir + '/data/trained_models') model.save(parentdir + '/data/trained_models/trained_model.h5') print('Model saved!') #save model in production format if extract_SavedModel: export_path = "./image_classifier/1/" with K.get_session() as sess: tf.saved_model.simple_save( sess, export_path, inputs={'input_image': model.input}, outputs={t.name: t for t in model.outputs}) else: self.model = model del history del model
except: model = Model(input=base_model.input, output=base_model.get_layer('fc1').output) image_size = (224, 224) elif model_name == "resnet50": base_model = ResNet50(weights=weights) try: model = Model(base_model.input, base_model.get_layer('avg_pool').output) except: model = Model(input=base_model.input, output=base_model.get_layer('avg_pool').output) image_size = (224, 224) elif model_name == "inceptionv3": base_model = InceptionV3(include_top=include_top, weights=weights, input_tensor=Input(shape=(299, 299, 3))) try: model = Model(base_model.input, base_model.get_layer('custom').output) except: model = Model(input=base_model.input, output=base_model.get_layer('custom').output) image_size = (299, 299) elif model_name == "inceptionresnetv2": base_model = InceptionResNetV2(include_top=include_top, weights=weights, input_tensor=Input(shape=(299, 299, 3))) try: model = Model(base_model.input, base_model.get_layer('custom').output)
from tensorflow.python.keras import layers from tensorflow.python.keras import Model import wget print("Inceptionv3 Weights is loading...") wget.download("https://storage.googleapis.com/mledu-datasets/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5", "/tmp/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5") print("Inceptionv3 Weights is loaded.") from tensorflow.python.keras.applications.inception_v3 import InceptionV3 local_weights_file = '/tmp/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5' pre_trained_model = InceptionV3(input_shape = (150, 150, 3), include_top = False, weights = None) pre_trained_model.load_weights(local_weights_file) for layer in pre_trained_model.layers: layer.trainable = False # pre_trained_model.summary() last_layer = pre_trained_model.get_layer('mixed7') print('last layer output shape: ', last_layer.output_shape) last_output = last_layer.output
import tensorflow as tf ###############################################################################################################3 #Δήλωση app = FastAPI() #Δήλωση μοντέλων μηχανικής μάθησης και οι αντίστοιχες ρυθμίσεις τους modelRes = ResNet50(weights='imagenet') modelVGG = VGG16(weights='imagenet', include_top=True) modelEfficient = EfficientNetB0(include_top=True, weights='imagenet') modelEfficientB7 = EfficientNetB7(include_top=True, weights='imagenet') modelNasNet = NASNetLarge(include_top=True, weights='imagenet') modelMobileNet = MobileNetV2(weights="imagenet", include_top=True) modelXception = Xception(include_top=True, weights="imagenet") modelInception = InceptionV3(include_top=True, weights="imagenet", classifier_activation="softmax") modelInRes = InceptionResNetV2(weights="imagenet", include_top=True) # Settings MIN_CONFIDENCE = 0.1 # Το ελάχιστο δυνατό confidence που δέχόμαστε απο τα μοντέλα. # Τα URLs που θα απαντάει το κάθε μοντέλο IMAGE_URL2 = "/v1/vision/resNet" IMAGE_URL3 = "/v1/vision/vggNet" IMAGE_URL4 = "/v1/vision/efficientNet" IMAGE_URL5 = "/v1/vision/nasNet" IMAGE_URL6 = "/v1/vision/mobileNet2" IMAGE_URL7 = "/v1/vision/xceptionNet" IMAGE_URL8 = "/v1/vision/efficientNetB7"
def run_model(args): # Configure the memory optimizer #gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.333) config = tf.ConfigProto() config.graph_options.rewrite_options.memory_optimization = rewriter_config_pb2.RewriterConfig.SCHEDULING_HEURISTICS #config.gpu_options.allow_growth=True config.gpu_options.per_process_gpu_memory_fraction = 0.5 K.set_session(tf.Session(config=config)) num_classes = args.num_classes batch_size = args.batch_size model_name = args.model if model_name == 'ResNet50': model = ResNet50(weights=None, include_top=True, input_shape=input_shape, classes=num_classes) elif model_name == 'ResNet101': model = keras.applications.resnet.ResNet101(weights=None, include_top=True, input_shape=input_shape, classes=num_classes) elif model_name == 'ResNet152': model = ResNet152(weights=None, include_top=True, input_shape=input_shape, classes=num_classes) elif model_name == 'VGG16': model = VGG16(weights=None, include_top=True, input_shape=input_shape, classes=num_classes) elif model_name == 'VGG19': model = VGG19(weights=None, include_top=True, input_shape=input_shape, classes=num_classes) elif model_name == 'Xception': model = Xception(weights=None, include_top=True, input_shape=input_shape, classes=num_classes) elif model_name == 'MobileNet': model = MobileNet(weights=None, include_top=True, input_shape=input_shape, classes=num_classes) elif model_name == 'MobileNetV2': model = MobilenetV2(weights=None, include_top=True, input_shape=input_shape, classes=num_classes) elif model_name == 'InceptionV3': model = InceptionV3(weights=None, include_top=True, input_shape=input_shape, classes=num_classes) else: print('Running with ResNet50 -- the default model') model = ResNet50(weights=None, include_top=True, input_shape=input_shape, classes=num_classes) execute_model(model, input_shape)
from tensorflow.python.keras import Input, layers from tensorflow.python.keras import optimizers from tensorflow.python.keras.models import Model from tensorflow.python.keras.layers import add from tensorflow.python.keras.preprocessing.sequence import pad_sequences from tensorflow.python.keras.utils import to_categorical import matplotlib.pyplot as plt START = "startseq" STOP = "endseq" root_captioning = "D:\\datasets\\image_caption_data" encode_model = InceptionV3(weights='imagenet') encode_model = Model(encode_model.input, encode_model.layers[-2].output) WIDTH = 299 HEIGHT = 299 OUTPUT_DIM = 2048 preprocess_input = tensorflow.keras.applications.inception_v3.preprocess_input test_path = os.path.join(root_captioning, "data", f'Vocab.pkl') with open(test_path, "rb") as fp: vocab = pickle.load(fp) idxtoword = {} wordtoidx = {} ix = 1 for w in vocab: