def __init__(self, config_file, model_name): self.configuration = conf.ConfigurationFile(config_file, model_name) #loading data mean_file = os.path.join(self.configuration.get_data_dir(), "mean.dat") shape_file = os.path.join(self.configuration.get_data_dir(), "shape.dat") # self.input_shape = np.fromfile(shape_file, dtype=np.int32) self.mean_image = np.fromfile(mean_file, dtype=np.float32) self.mean_image = np.reshape(self.mean_image, self.input_shape) #loading classifier model # self.model = resnet.ResNetFeatureVector([3,4,6,3],[64,128,256,512], se_factor = 0) input_image = tf.keras.Input( (self.input_shape[0], self.input_shape[1], self.input_shape[2]), name='input_image') # self.model(input_image) # self.model.summary() # self.model.load_weights(self.configuration.get_checkpoint_file(), by_name = True, skip_mismatch = True) #color model model = resnet.ResNetFeatureVector([3, 4, 6, 3], [64, 128, 256, 512], se_factor=0) model(input_image) model.summary() model.load_weights(self.configuration.get_checkpoint_file(), by_name=True, skip_mismatch=True) #create the sim-model with a customized layer output1 = model.output output2 = model.get_layer('backbone').get_layer('block_1').output output2 = tf.keras.layers.GlobalAveragePooling2D()(output2) output = tf.concat([output1, output2], axis=1) self.model_color = tf.keras.Model(model.input, output) self.model_color.summary() self.model_color.save('color_model') print('model was loaded OK') #defining process arch self.process_fun = imgproc.process_image #loading catalog self.ssearch_dir = os.path.join(self.configuration.get_search_dir(), 'ssearch') catalog_file = os.path.join(self.ssearch_dir, 'catalog.txt') assert os.path.exists(catalog_file), '{} does not exist'.format( catalog_file) print('loading catalog ...') self.load_catalog(catalog_file) print('loading catalog ok ...') sys.stdout.flush() self.enable_search = False
def __init__(self, config_file, model_name): self.configuration = conf.ConfigurationFile(config_file, model_name) #loading data mean_file = os.path.join(self.configuration.get_data_dir(), "mean.dat") shape_file = os.path.join(self.configuration.get_data_dir(), "shape.dat") # self.input_shape = np.fromfile(shape_file, dtype=np.int32) self.mean_image = np.fromfile(mean_file, dtype=np.float32) self.mean_image = np.reshape(self.mean_image, self.input_shape) #loading classifier model model = resnet.ResNet([3, 4, 6, 3], [64, 128, 256, 512], self.configuration.get_number_of_classes(), se_factor=0) input_image = tf.keras.Input( (self.input_shape[0], self.input_shape[1], self.input_shape[2]), name='input_image') model(input_image) model.summary() model.load_weights(self.configuration.get_checkpoint_file(), by_name=True, skip_mismatch=True) #create the sim-model with a customized layer #you can change output_layer_name output_layer_name = 'global_average_pooling2d' output = model.get_layer(output_layer_name).output self.sim_model = tf.keras.Model(model.input, output) self.sim_model.summary() print('sim_model was loaded OK') #defining process arch self.process_fun = imgproc.process_image #loading catalog self.ssearch_dir = os.path.join(self.configuration.get_data_dir(), 'ssearch') catalog_file = os.path.join(self.ssearch_dir, 'catalog.txt') assert os.path.exists(catalog_file), '{} does not exist'.format( catalog_file) print('loading catalog ...') self.load_catalog(catalog_file) print('loading catalog ok ...') self.enable_search = False
img_array, (width, height)) # resize image to match model's expected sizing res = new_array.reshape(-1, height, width, 3) res = res - mean_image return res # return the image with shaping that TF wants. disable_eager_execution() model = tf.keras.models.load_model( 'C:/Users/andyb/PycharmProjects/kerasResnet/resnet50.model', compile=True) configuration_file = "configs/cnn.config" configuration = conf.ConfigurationFile(configuration_file, "RES") shape_file = os.path.join(configuration.get_data_dir(), "shape.dat") mean_file = os.path.join(configuration.get_data_dir(), "mean.dat") input_shape = np.fromfile(shape_file, dtype=np.int32) mean_image = np.fromfile(mean_file, dtype=np.float32) mean_image = np.reshape(mean_image, input_shape) img_name = "4090" img_path = "C:/Users/andyb/PycharmProjects/kerasResnet/images390p/" + img_name + ".jpg" img = image.load_img(img_path, target_size=(input_shape[0], input_shape[1])) x = image.img_to_array(img) x = np.expand_dims(x, axis=0)
import sys sys.path.append("/data/practica/otra/") import tensorflow as tf from models import resnet import utils.configuration as conf import numpy as np import argparse if __name__ == '__main__' : parser = argparse.ArgumentParser(description = "Train a simple mnist model") parser.add_argument("-config", type = str, help = "<str> configuration file", required = True) parser.add_argument("-name", type=str, help=" name of section in the configuration file", required = True) parser.add_argument("-image_type", type=str, help=" sketch or photo", choices =['sketch', 'image'], required = True) pargs = parser.parse_args() configuration_file = pargs.config configuration = conf.ConfigurationFile(configuration_file, pargs.name) number_of_classes = configuration.get_number_of_classes() shape_file = os.path.join(configuration.get_data_dir(),'sketches', 'shape.dat') input_shape = np.fromfile(shape_file, dtype=np.int32) if pargs.image_type == 'sketch' : model = resnet.SiameseNetSketch([3,4,6,3],[64,128,256,512], False, se_factor = 0) else: model = resnet.SiameseNetImage([3,4,6,3],[64,128,256,512], False, se_factor = 0) input_image = tf.keras.Input((input_shape[0], input_shape[1], input_shape[2]), name = 'input_' + pargs.image_type) model(input_image) model.summary() assert configuration.use_checkpoint() model.load_weights(configuration.get_checkpoint_file(), by_name = True, skip_mismatch = True)