def reduce_model(model_path): from kito import reduce_keras_model from keras.models import load_model m = load_model(model_path) m_red = reduce_keras_model(m) m_red.save(model_path[:-3] + '_reduced.h5')
def createImageEmbeddingModel(): import keras, kito model = keras.applications.MobileNetV2() model.summary() outLayer = model.get_layer('global_average_pooling2d_1') #return keras.Model(model.inputs, outLayer.output) return kito.reduce_keras_model(keras.Model(model.inputs, outLayer.output))
def __init__(self, img_size=64, depth=16, width=8, pretrained_model="weights.29-3.76_utk.hdf5"): self.img_size = img_size self.model = WideResNet(img_size, depth=depth, k=width)() self.model.load_weights(pretrained_model) self.model = reduce_keras_model(self.model)
img_list = pickle.load(img_f) with open(output_path[1], 'rb') as label_f: label_list = pickle.load(label_f) mean_arr = None # np.zeros(input_shape) #for img in img_list: # mean_arr += img.astype('float32') #mean_arr /= len(img_list) #print('mean shape:',mean_arr.shape, 'mean mean:',mean_arr.mean(), 'mean max:',mean_arr.max()) #mean_arr /= 255 #np.save('./mean.npy', mean_arr) if config.pause: nsml.paused(scope=locals()) bTrainmode = False if config.mode == 'train': bTrainmode = True #nsml.load(checkpoint='86', session='Zonber/ir_ph1_v2/204') #Nasnet Large 222 nsml.load(checkpoint='0', session='Zonber/ir_ph2/222') #InceptionResnetV2 222 print('convert start model') intermediate_layer_model = Model( inputs=model.input[0], outputs=model.get_layer('triplet_loss_layer').input[0]) model_r = reduce_keras_model(intermediate_layer_model) model_r.summary() print('convert complete reduce model') bind_model(model_r) print('binde reduce model complete') nsml.save(0) # this is display model name at lb
merge_avg = False model_list = get_best_model_list(5, MODELS_PATH_KERAS) model_list_reduced = [] for model_path in model_list: if model_path == '': model_list_reduced.append(None) continue store_path = model_path[:-3] + '_reduced.h5' if not os.path.isfile(store_path): model, _ = Model_3D_pretrained_densenet121(input_shape=SHAPE_SIZE, dropout_val=0.2, out_channels=1) print('Load weights: {}'.format(model_path)) model.load_weights(model_path) model = reduce_keras_model(model, verbose=True) model.save(store_path) model = None gc.collect() clear_session() model_list_reduced.append(store_path) _, preproc_input = Model_3D_pretrained_densenet121(input_shape=SHAPE_SIZE, dropout_val=0.1, out_channels=1) score1, score2, score3, out_feat_file = valid_model( model_list_reduced, preproc_input) merged_model = merge_models( SHAPE_SIZE, model_list_reduced, MODELS_PATH_KERAS +
#log_dir = 'logs/logits_only_000/' #classes_path = 'class/voc_classes.txt' #anchors_path = 'anchors/tiny_yolo_anchors.txt' classes_path = 'class/elderly_classes.txt' anchors_path = 'anchors/elderly_anchors.txt' class_names = get_classes(classes_path) num_classes = len(class_names) anchors = get_anchors(anchors_path) #input_shape = (416,416) # multiple of 32, hw #ima#ge_input = Input(shape=(416,416, 3)) image_input = Input(shape=(None, None, 3)) #h, w = input_shape num_anchors = len(anchors) model = yolo_body(image_input, 3, num_classes) model.load_weights(model_path) model.summary() print(len(model.layers)) model_reduced = reduce_keras_model(model) #model_reduced.save('model_data/416bnfuse_small_mobilenets2_trained_model.h5') #model_reduced.save('model_data/416bnfuse_tiny_yolo.h5') #model_reduced.save('model_data/bnfuse_med_tiny_yolo.h5') #model_reduced.save('model_data/bnfuse_a2_ds_small_mobilenets2_trained_model.h5') model_reduced.save( 'model_data/bnfuse_eld_small_mobilenets2_trained_weights_final.h5') model_reduced.summary() print(len(model_reduced.layers))