def main(model, train_file, valid_file, ckpt_folder, optimizer, batch_size, max_steps, lr, l2, fine_tuning): if optimizer == 'Adam': optimizer = Adam(lr=lr) if l2 > 0: regularizer = regularizers.l2(l2) else: regularizer = None metrics = ['categorical_accuracy'] loss = 'categorical_crossentropy' if model == 'VGG16Trunk': model_fn = VGG16Trunk(IMAGE_SHAPE, INPUT_NAME, optimizer, loss, metrics, fine_tuning) elif model == 'XceptionTrunk': model_fn = Xception(IMAGE_SHAPE, INPUT_NAME, optimizer, loss, metrics, fine_tuning) model_fn.summary() # Start training my_train_and_evaluate(model_fn=model_fn, train_file=train_file, valid_file=valid_file, ckpt_folder=ckpt_folder, batch_size=batch_size, max_steps=max_steps)
def XceptionTrunk(image_shape, input_name, optimizer, loss, metrics, fine_tuning=False): x = Input(shape=image_shape, name=input_name) base_model = Xception(weights='imagenet', include_top=False, input_tensor=x) for layer in base_model.layers: layer.trainable = False a = Flatten()(conv_base) a = Dense(1024, activation='relu')(a) a = Dropout(0.5)(a) y = Dense(NUM_CLASSES, activation='softmax')(a) model = Model(inputs=x, outputs=y) model.compile(loss=loss, optimizer=optimizer, metrics=metrics) return model
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 cnn_model(labels): # include_top=Falseによって、モデルから全結合層を削除 input_tensor = Input(shape=(299, 299, 3)) # input_tensor = GaussianNoise(stddev=0.1)(input_tensor) # xception_model = Xception(include_top=False, input_shape=(299, 299, 3), pooling='avg') xception_model = Xception(include_top=False, pooling='avg', input_tensor=input_tensor) # 全結合層の構築 top_model = Sequential() # top_model.add(Flatten(input_shape=xception_model.output_shape[1:])) # top_model.add(Activation("relu")) # top_model.add(Dropout(0.3)) # top_model.add(Dense(1024,)) # top_model.add(Activation("softmax")) # top_model.add(BatchNormalization(input_shape=xception_model.output_shape[1:])) top_model.add(GaussianNoise(stddev=0.2,input_shape=xception_model.output_shape[1:])) top_model.add(Dropout(0.3)) top_model.add(Dense(len(labels),input_shape=xception_model.output_shape[1:])) top_model.add(Activation("softmax")) # 全結合層を削除したモデルと上で自前で構築した全結合層を結合 model = Model(inputs=xception_model.input, outputs=top_model(xception_model.output)) # 図3における14層目までのモデル重みを固定(VGG16のモデル重みを用いる) # print('model.layers:',len(xception_model.layers)) # xception_model.trainable = False for layer in xception_model.layers[:-50]: layer.trainable = False return model
def build_model(dropout, learning_rate): input = Input(shape=(HEIGHT, WIDTH, 3)) base_model = Xception(input_tensor=input, weights='imagenet', include_top=False, pooling='avg') for layer in base_model.layers: layer.trainable = True x = base_model.output x = Dropout(dropout)(x) x = Dense(NUMBER_OF_CLASSES, activation='softmax')(x) model = Model(inputs=input, outputs=x) model.compile(optimizer=Adam(lr=learning_rate), loss='categorical_crossentropy', metrics=['categorical_accuracy']) return model
def CrossModalityXception(num_classes, pre_trained, cross_modality_pre_training, input_shape, include_feature_fields=False): cross_modality_pre_training = cross_modality_pre_training and pre_trained # create the model model = Xception(classes=num_classes, weights=None, input_shape=input_shape, include_top=True) channels = input_shape[2] # load weight file >>> downloads some file from github weights_path = get_file( 'xception_weights_tf_dim_ordering_tf_kernels_notop.h5', TF_WEIGHTS_PATH_NO_TOP, cache_subdir='models', file_hash='b0042744bf5b25fce3cb969f33bebb97') weight_values_ = get_named_layer_weights_from_h5py(weights_path) symbolic_weights_ = get_symbolic_filtered_layer_weights_from_model( model)[:len(weight_values_)] if cross_modality_pre_training: # use a pretrained convolution weight # update it (name,[kernel,bias]) # cross modality pre-training for kernel # leave bias as is of course weight_values_[0] = ( "conv1_cross_modality", [ cross_modality_init( kernel=weight_values_[0][1][0], in_channels=channels ), # 0 = first layer , 1 = weight_value , 0 = kernel # Xception has no bias ]) else: # start the first convolution layer as random glorot symbolic_weights_ = symbolic_weights_[1:] weight_values_ = weight_values_[1:] if pre_trained: # do weight loading load_layer_weights(weight_values=weight_values_, symbolic_weights=symbolic_weights_) if include_feature_fields: return Model(model.inputs, [layer.output for layer in model.layers[-2:]]) else: return model
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
train_x, train_y = supplement_training_data(aug, train_x, train_y) print("[INFO] Training data shape: " + str(train_x.shape)) print("[INFO] Training label shape: " + str(train_y.shape)) # convert the labels from integers to vectors (for 2-class, binary # classification you should use Keras' to_categorical function # instead as the scikit-learn's LabelBinarizer will not return a # vector) lb = LabelBinarizer() train_y = lb.fit_transform(train_y) test_y = lb.transform(test_y) base_model = Xception(input_shape=IMAGE_DIMS, classes=len(data_set.class_names), weights=None, include_top=False) model = create_classification_layers(base_model, classes=len(data_set.class_names), dropout_prob=hyperparameters.dropout) opt = SGD(learning_rate=hyperparameters.init_lr, decay=True) model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy']) print('[INFO] Adding callbacks') callbacks = create_callbacks() # train the network H = model.fit(x=aug.flow(train_x,
from helpers import classify_image, read_labels, set_input_tensor 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"
output=base_model.get_layer('custom').output) image_size = (299, 299) elif model_name == "mobilenet": base_model = MobileNetV2(include_top=include_top, weights=weights, input_tensor=Input(shape=(224, 224, 3)), input_shape=(224, 224, 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 = (224, 224) elif model_name == "xception": base_model = Xception(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 = (299, 299) else: base_model = None print("loaded base model and model...") # path to training dataset train_labels = os.listdir(train_path)
test_gen = ImageDataGenerator(featurewise_center=True, featurewise_std_normalization=True) test_gen.fit(X_test) test_flow = test_gen.flow(X_test, y_test, batch_size=16, shuffle=False) ############ MODEL ARCHITECTURE ############# #.inception_resnet_v2 import InceptionResNetV2 .resnet_v2 import ResNet50V2 .xception import Xception from tensorflow.python.keras.applications.xception import Xception from tensorflow.python.keras.models import Sequential, Model from tensorflow.python.keras.layers import Dense, Dropout, GlobalAveragePooling2D, AveragePooling2D, Input input_tensor = Input(shape=(256, 256, 3)) #InceptionResNetV2 ResNet50V2 Xception base_model = Xception(weights='imagenet', include_top=False, pooling='avg', input_tensor=input_tensor) x = base_model.output x = Dense(512, activation='relu')(x) predictions = Dense(1, activation='sigmoid')(x) model = Model(inputs=base_model.input, outputs=predictions) # Say not to train ResNet layers for layer in base_model.layers: layer.trainable = True ################# COMPILE MODEL ################# from tensorflow.keras.optimizers import Adam, SGD, RMSprop model.compile(loss='binary_crossentropy', optimizer=Adam(lr=1e-4), metrics=['binary_accuracy'])
def fit(self, learning_rate=1e-4, epochs=5, activation='relu', dropout=0, hidden_size=1024, nb_layers=1, include_class_weight=False, save_augmented=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, cutoff_regularization=False, extract_SavedModel=False): #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)) target_size = (299, 299) elif transfer_model == 'Inception_Resnet': base_model = InceptionResNetV2(weights='imagenet', include_top=False, input_shape=(299, 299, 3)) target_size = (299, 299) elif transfer_model == 'Resnet': base_model = ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3)) target_size = (224, 224) else: base_model = InceptionV3(weights='imagenet', include_top=False, input_shape=(299, 299, 3)) target_size = (299, 299) #We expect the classes to be the name of the folders in the training set self.categories = os.listdir(TRAIN_DIR) #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']) datagen_train = ImageDataGenerator(rotation_range=180, rescale=1. / 255, width_shift_range=0.1, height_shift_range=0.1, shear_range=0.1, zoom_range=[0.9, 1.5], horizontal_flip=True, vertical_flip=True, fill_mode='nearest') datagen_val = ImageDataGenerator(rescale=1. / 255) #Save the augmented images if we want to if save_augmented: save_to_dir = AUGMENTED_DIR else: save_to_dir = None self.generator_train = datagen_train.flow_from_directory( directory=TRAIN_DIR, target_size=target_size, batch_size=batch_size, shuffle=True, save_to_dir=save_to_dir) self.generator_val = datagen_val.flow_from_directory( directory=VAL_DIR, target_size=target_size, batch_size=batch_size, shuffle=False) steps_per_epoch = self.generator_train.n / batch_size self.val_steps_per_epoch = self.generator_val.n / batch_size #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.generator_train.classes class_weight = compute_class_weight(class_weight='balanced', classes=np.unique(cls_train), y=cls_train) else: class_weight = None #Fit the model history = model.fit_generator( generator=self.generator_train, epochs=epochs, steps_per_epoch=steps_per_epoch, verbose=verbose, class_weight=class_weight, validation_data=self.generator_val, validation_steps=self.val_steps_per_epoch, callbacks=callback) #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 history = model.fit_generator( generator=self.generator_train, epochs=epochs, steps_per_epoch=steps_per_epoch, verbose=verbose, class_weight=class_weight, validation_data=self.generator_val, validation_steps=self.val_steps_per_epoch) #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
predictions = Dense(n_classes, activation='softmax')(x) model = Model(inputs=base_model.input, outputs=predictions) return model if __name__ == '__main__': # Hyperparameter setting batch_size = config.batch_size num_classes = config.nb_classes epochs = config.epochs # Get the path of current running file pwd = os.path.dirname(os.path.abspath(__file__)) # Construct Xception model with our own top base_model = Xception( include_top=False, weights=pwd + '/xception_weights_tf_dim_ordering_tf_kernels_notop.h5', input_shape=(image_size, image_size, 3)) model = add_new_classifier(base_model) # Unfreeze all layers # for layer in base_model.layers: # layer.trainable = False print("Xception!!!") # print(model.summary()) # compile model model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=config.lr, decay=1e-6), metrics=['accuracy']) # Data Augmentation: datagen = ImageDataGenerator(
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
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
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))
is_tesla_k80=True) model2 = ResNet50MotionCNN2(num_classes=10, stacked_frames=10, is_tesla_k80=True) model3 = ResNet50() print(model1.layers) print(model2.layers) print(model3.layers) print(" ") compare_layers_weights(model1.layers[1].layers, model2.layers[1].layers) print(" ") compare_layers_weights(model3.layers, model2.layers[1].layers) print(" ") compare_layers_weights(model3.layers, model1.layers[1].layers) print(" ") print("xception test") model4 = Xception(input_shape=(299, 299, 3)) model5 = XceptionMotionCNN(num_classes=10, is_tesla_k80=True, stacked_frames=10) print(model4.layers) print(model5.layers) compare_layers_weights(model4.layers, model5.layers[1].layers) print("values") print(model4.layers[1].weights) print(model4.layers[1].get_weights()[0][0, 0, :, 0]) print(model5.layers[1].layers[1].get_weights()[0][0, 0, :, 0])
def xception_feature_extractor(): inp = Input(shape=(None, None, 3), name='image_input') return Xception(include_top=False, input_tensor=inp, pooling='avg', weights='imagenet')
def create_model(model_name, input_shape, target_labels, n_classes_per_label_type, n_gpus, continue_training=False, rebuild_model=False, transfer_learning=False, transfer_learning_type='last_layer', path_of_model_to_load=None, initial_learning_rate=0.01, output_loss_weights=None, optimizer='sgd'): """ Returns specified model architecture """ # Load model from disk if continue_training and not rebuild_model: logging.debug("Preparing continue_training") loaded_model = load_model_from_disk(path_of_model_to_load) return loaded_model model_input = Input(shape=input_shape, name='images') if model_name == 'InceptionResNetV2': keras_model = InceptionResNetV2(include_top=False, weights=None, input_tensor=model_input, input_shape=None, pooling='avg') output_flat = keras_model.output model_input = keras_model.input elif model_name in [ 'ResNet18', 'ResNet34', 'ResNet50', 'ResNet101', 'ResNet152' ]: res_builder = ResnetBuilder() if model_name == 'ResNet18': output_flat = res_builder.build_resnet_18(model_input) elif model_name == 'ResNet34': output_flat = res_builder.build_resnet_34(model_input) elif model_name == 'ResNet50': output_flat = res_builder.build_resnet_50(model_input) elif model_name == 'ResNet101': output_flat = res_builder.build_resnet_101(model_input) elif model_name == 'ResNet152': output_flat = res_builder.build_resnet_152(model_input) elif model_name == 'small_cnn': output_flat = small_cnn(model_input) elif model_name == 'Xception': keras_model = Xception(include_top=False, weights=None, input_tensor=model_input, input_shape=None, pooling='avg') output_flat = keras_model.output model_input = keras_model.input else: raise ValueError("Model: %s not implemented" % model_name) all_target_outputs = list() for n_classes, target_name in zip(n_classes_per_label_type, target_labels): all_target_outputs.append( Dense(units=n_classes, kernel_initializer="he_normal", activation='softmax', name=target_name)(output_flat)) # Define model optimizer if optimizer == 'sgd': opt = SGD(lr=initial_learning_rate, momentum=0.9, decay=1e-4) elif optimizer == 'rmsprop': opt = RMSprop(lr=0.01, rho=0.9, epsilon=1e-08, decay=0.0) else: raise ValueError("optimizer %s not implemented" % optimizer) if n_gpus > 1: logging.debug("Preparing Multi-GPU Model") with tf.device('/cpu:0'): base_model = Model(inputs=model_input, outputs=all_target_outputs) if continue_training and rebuild_model: logging.debug("Preparing continue_training by \ rebuilding model") loaded_model = load_model_from_disk(path_of_model_to_load) copy_model_weights(loaded_model, base_model, incl_last=True) elif transfer_learning: if transfer_learning_type == 'last_layer': logging.debug("Preparing transfer_learning with freezing \ all but the last layer") loaded_model = load_model_from_disk(path_of_model_to_load) copy_model_weights(loaded_model, base_model, incl_last=False) non_output_layers = get_non_output_layer_ids(base_model) base_model = set_layers_to_non_trainable( base_model, non_output_layers) elif transfer_learning_type == 'all_layers': logging.debug("Preparing transfer_learning with freezing \ no layers") loaded_model = load_model_from_disk(path_of_model_to_load) copy_model_weights(loaded_model, base_model, incl_last=False) else: raise ValueError("transfer_learning_type option %s not \ recognized" % transfer_learning) model = multi_gpu_model(base_model, gpus=n_gpus) else: model = Model(inputs=model_input, outputs=all_target_outputs) if continue_training and rebuild_model: logging.debug("Preparing continue_training by \ rebuilding model") loaded_model = load_model_from_disk(path_of_model_to_load) copy_model_weights(loaded_model, model, incl_last=True) elif transfer_learning: if transfer_learning_type == 'last_layer': logging.debug("Preparing transfer_learning with freezing \ all but the last layer") loaded_model = load_model_from_disk(path_of_model_to_load) copy_model_weights(loaded_model, model, incl_last=False) non_output_layers = get_non_output_layer_ids(model) model = set_layers_to_non_trainable(model, non_output_layers) elif transfer_learning_type == 'all_layers': logging.debug("Preparing transfer_learning with freezing \ no layers") loaded_model = load_model_from_disk(path_of_model_to_load) copy_model_weights(loaded_model, model, incl_last=False) else: raise ValueError("transfer_learning_type option %s not \ recognized" % transfer_learning) model.compile(loss=build_masked_loss(K.sparse_categorical_crossentropy), optimizer=opt, loss_weights=output_loss_weights, metrics=[accuracy, top_k_accuracy]) return model
classifiers = ['VGG19', 'ResNet50', 'Xception'] logmels = [] X = [] y = [] for (_, dirs, filenames) in os.walk(features_path): if chunked: for d in dirs: files = os.listdir(os.path.join(features_path, d)) for f in files: logmels.append(os.path.join(d, f)) else: logmels.extend(filenames) break for c in classifiers: if c == 'VGG19': model = VGG19(weights='imagenet', include_top=False, pooling='avg') elif c == 'ResNet50': model = ResNet50(weights='imagenet', include_top=False, pooling='avg') elif c == 'Xception': model = Xception(weights='imagenet', include_top=False, pooling='avg') for lm in logmels: X.append(get_features(os.path.join(features_path, lm), classifier=c)) y.append(0) print('Done')
def create_model(self): """ https://github.com/tensorflow/tensorflow/issues/14356 """ input_shape = (self.config.tfr_image_height, self.config.tfr_image_width, self.config.tfr_image_channels) ## VGG16 if self.config.model_name == 'vgg16': base_model = VGG16(weights='imagenet', include_top=False, input_tensor=self.features, input_shape=input_shape) ## Xception elif self.config.model_name == 'xception': base_model = Xception(weights='imagenet', include_top=False, input_tensor=self.features, input_shape=input_shape) ## Resnet50 elif self.config.model_name == 'resnet50': base_model = ResNet50(weights='imagenet', include_top=False, input_tensor=self.features, input_shape=input_shape) logits = self.model_top_resnet50(base_model) ## InceptionResNetV2 elif self.config.model_name == 'inception_resnet_v2': base_model = InceptionResNetV2(weights='imagenet', include_top=False, input_tensor=self.features, input_shape=input_shape) ## Densenet121 elif self.config.model_name == 'densenet121': base_model = DenseNet121(weights='imagenet', include_top=False, input_tensor=self.features, input_shape=input_shape) logits = self.model_top_densenet121(base_model) ## Densenet169 elif self.config.model_name == 'densenet169': base_model = DenseNet169(weights='imagenet', include_top=False, input_tensor=self.features, input_shape=input_shape) logits = self.model_top_densenet121(base_model) ## Densenet201 elif self.config.model_name == 'densenet201': base_model = DenseNet201(weights='imagenet', include_top=False, input_tensor=self.features, input_shape=input_shape) logits = self.model_top_densenet121(base_model) else: logging.error('Unknown model_name {}'.format(model_name)) exit(1) return logits
save_images_from_wnid(cwd, categ_name, wnid, 12) categ_names.append(categ_name) # ResNet50 from tensorflow.python.keras.applications.resnet50 import ResNet50, preprocess_input, decode_predictions # Load the model architecture and the imagenet weights for the networks model = ResNet50(weights='imagenet') #model.summary() #model.get_weights()[0] cmResNet50 = predictNevaluate(model, (224, 224), cwd, categ_names, wnids_list) # VGG16 from tensorflow.python.keras.applications.vgg16 import VGG16, preprocess_input, decode_predictions model = VGG16(weights='imagenet') cmVGG16 = predictNevaluate(model, (224, 224), cwd, categ_names, wnids_list) # MobileNet from tensorflow.python.keras.applications.mobilenet import MobileNet, preprocess_input, decode_predictions model = MobileNet(weights='imagenet') cmMobileNet = predictNevaluate(model, (224, 224), cwd, categ_names, wnids_list) # Inception_V3 from tensorflow.python.keras.applications.inception_v3 import InceptionV3, preprocess_input, decode_predictions model = InceptionV3(weights='imagenet') cmInception_V3 = predictNevaluate(model, (299, 299), cwd, categ_names, wnids_list) # Xception from tensorflow.python.keras.applications.xception import Xception, preprocess_input, decode_predictions model = Xception(weights='imagenet') cmXception = predictNevaluate(model, (299, 299), cwd, categ_names, wnids_list)
def __init__(self, input_size=(512, 512, 1), should_load_model=False, use_transfer_learning_ensemble=False, train_split=0.85): config = ConfigProto() config.gpu_options.allow_growth = True session = InteractiveSession(config=config) self.gen = image_generator.Generator(train_split) self.input_size = input_size self.models = [] if not should_load_model: if not use_transfer_learning_ensemble: inputs = Input(input_size) conv1 = Conv2D(8, 3, input_shape=self.input_size, activation='relu', padding='same', kernel_initializer='he_normal')(inputs) conv2 = Conv2D(8, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv1) merge1 = concatenate([conv1, conv2], axis=3) mp1 = MaxPooling2D((2, 2))(merge1) drop1 = Dropout(0.2)(mp1) conv3 = Conv2D(8, 3, activation='relu', padding='same', kernel_initializer='he_normal')(drop1) conv4 = Conv2D(8, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv3) merge2 = concatenate([conv3, conv4], axis=3) mp2 = MaxPooling2D((2, 2))(merge2) drop2 = Dropout(0.35)(mp2) conv5 = Conv2D(16, 3, activation='relu', padding='same', kernel_initializer='he_normal')(drop2) conv6 = Conv2D(16, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv5) merge3 = concatenate([conv5, conv6], axis=3) mp3 = MaxPooling2D((2, 2))(merge3) conv7 = Conv2D(16, 3, activation='relu', padding='same', kernel_initializer='he_normal')(mp3) conv8 = Conv2D(16, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv7) merge3 = concatenate([conv7, conv8], axis=3) conv9 = Conv2D(32, 3, activation='relu', padding='same', kernel_initializer='he_normal')(merge3) conv10 = Conv2D(32, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv9) merge4 = concatenate([conv9, conv10], axis=3) mp4 = MaxPooling2D((2, 2))(merge4) conv11 = Conv2D(32, 3, activation='relu', padding='same', kernel_initializer='he_normal')(mp4) conv12 = Conv2D(32, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv11) merge5 = concatenate([conv11, conv12], axis=3) conv13 = Conv2D(64, 3, activation='relu', padding='same', kernel_initializer='he_normal')(merge5) conv14 = Conv2D(64, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv13) merge6 = concatenate([conv13, conv14], axis=3) conv15 = Conv2D(64, 3, activation='relu', padding='same', kernel_initializer='he_normal')(merge6) conv16 = Conv2D(64, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv15) merge7 = concatenate([conv15, conv16], axis=3) mp5 = MaxPooling2D((2, 2))(merge7) drop4 = Dropout(0.45)(mp5) flat = Flatten()(drop4) dense1 = Dense(5, activation='softmax')(flat) self.models.append(Model(inputs, dense1)) else: xception_base_model = Xception(include_top=False, weights="imagenet", input_shape=(512, 512, 3)) # for layer in xception_base_model.layers: # layer.trainable = False x = Flatten()(xception_base_model.output) x = Dropout(0.25)(x) xception_predictions = Dense(3, activation='softmax')(x) self.models.append( Model(inputs=xception_base_model.input, outputs=xception_predictions)) # # inception_base_model = InceptionV3(include_top=False, weights="imagenet", input_shape=(512, 512, 3)) # # for layer in inception_base_model.layers: # layer.trainable = False # # x = Flatten()(inception_base_model.output) # x = Dropout(0.25)(x) # inception_predictions = Dense(3, activation='softmax')(x) # # self.models.append(Model(inputs=inception_base_model.input, outputs=inception_predictions)) # resnet_base_model = ResNet50(include_top=False, weights="imagenet", input_shape=(512, 512, 3)) # # for layer in resnet_base_model.layers: # layer.trainable = False # # x = Flatten()(resnet_base_model.output) # x = Dropout(0.25)(x) # resnet_predictions = Dense(3, activation='softmax')(x) # self.models.append(Model(inputs=resnet_base_model.input, outputs=resnet_predictions)) for model in self.models: model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['sparse_categorical_accuracy']) else: for root, dirs, files in os.walk( 'diagnosis_pipeline/saved_models'): for file in files: file_path = os.path.join(root, file) self.models.append(load_model(file_path)) for model in self.models: model.summary()
train_labels.append(0) for i in range(len(os.listdir(path2))): train_image = cv2.imread(path2 + str(i) + '.png') train_image = cv2.cvtColor(train_image, cv2.COLOR_BGR2RGB) train_image = cv2.resize(train_image, (h, w)) train_images.append(train_image) train_labels.append(1) train_images = np.array(train_images) X_train, X_test, y_train, y_test = train_test_split( train_images, to_categorical(train_labels), test_size=0.2, random_state=42) base_model = Xception(weights='imagenet', include_top=False, input_shape=(h, w, 3), pooling='avg') base_model.trainable = False model = Sequential([base_model, Dense(2, activation='softmax')]) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) print(model.summary()) batch_size = 16 epochs = 5
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)
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: