def reset_layer_names(args): '''In case of transfer learning, it's important that the names of the weights match between the different networks (e.g. 2X and 4X). This function loads the lower-lever SR network from a reset keras session (thus forcing names to start from naming index 0), loads the weights onto that network, and saves the weights again with proper names''' # Find lower-upscaling model results BASE_G = os.path.join( args.weight_path, 'VSRGANplus' + args.modelname + '_generator_' + str(args.scaleFrom) + 'X.h5') BASE_D = os.path.join( args.weight_path, 'VSRGANplus' + args.modelname + '_discriminator_' + str(args.scaleFrom) + 'X.h5') assert os.path.isfile(BASE_G), 'Could not find ' + BASE_G assert os.path.isfile(BASE_D), 'Could not find ' + BASE_D # Load previous model with weights, and re-save weights so that name ordering will match new model prev_gan = VSRGANplus(upscaling_factor=args.scaleFrom) prev_gan.load_weights(BASE_G, BASE_D) prev_gan.save_weights(args.weight_path + 'VSRGANplus{}'.format(args.modelname)) del prev_gan K.reset_uids() gc.collect() return BASE_G, BASE_D
def predict_koa(self): K.reset_uids() model = "models\model.json" weights = "models/model_weight.h5" classes = { 'TRAIN': ['GRADE 0', 'GRADE 1', 'GRADE 2', 'GRADE 3', 'GRADE 4'], 'VALIDATION': ['GRADE 0', 'GRADE 1', 'GRADE 2', 'GRADE 3', 'GRADE 4'], 'TEST': ['GRADE 0', 'GRADE 1', 'GRADE 2', 'GRADE 3', 'GRADE 4'], } with CustomObjectScope({'GlorotUniform': glorot_uniform()}): with open(model, 'r') as f: model = model_from_json(f.read()) model.load_weights(weights) img = image.load_img(self.img, target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) model.compile(loss="categorical_crossentropy", metrics=[ "accuracy"], optimizer="adam") result = model.predict(x) pred_name = classes['TRAIN'][np.argmax(result)] print("The osteoarthritis is classified to be {}.".format(pred_name)) return pred_name
def cargar_rnn(nombreArchivoModelo, nombreArchivoPesos): k.reset_uids() # Cargar la Arquitectura desde el archivo JSON with open(nombreArchivoModelo + '.json', 'r') as f: model = model_from_json(f.read()) # Cargar Pesos (weights) en el nuevo modelo model.load_weights(nombreArchivoPesos + '.h5') return model
def __init__(self, image_size, is_learning_phase=False): K.set_learning_phase(int(is_learning_phase)) K.reset_uids() self.image_size = image_size self.n_cells = self.image_size // 32 self.m = self.buildModel() self.model_compile()
def trainModels(nameList, shutDown): for modelSet in modelSets: print("Training {0}".format(modelSet.name)) model = modelsDict[modelSet.name] trainModel(model.structure, model.generator, modelClass = modelSet.modelClass, epochs = model.epochs, losses = model.losses, modelName = modelSet.name, outClasses = model.outClasses, outVectors = model.outVectors, learning_rate = model.lr, metrics = model.metrics, altLabels = model.altLabels, augmentation = model.augmentation) K.clear_session() K.reset_uids() if shutDown: os.system('shutdown -s')
def __init__(self,encoder, image_size, n_classes): self.encoder_output = encoder.model.output self.anchors = np.array([ 1.08, 1.19, 3.42, 4.41, 6.63, 11.38, 9.42, 5.11, 16.62, 10.52 ]).reshape(5, 2) K.reset_uids() self.image_size = image_size self.n_cells = self.image_size // 32 self.B = self.anchors.shape[0] self.n_classes = n_classes self.model = self.make_model() self.num_layers = ModelBlock.get_head_num_layers(encoder, self.model)
def cargar_rnn(nombreArchivoModelo, nombreArchivoPesos): k.reset_uids() with open(nombreArchivoModelo + '.json', 'r') as f: model = model_from_json(f.read()) model.load_weights(nombreArchivoPesos + '.h5') return model
def wrapped(*args, **kwargs): K.clear_session() K.reset_uids() return func(*args, **kwargs)