Esempio n. 1
0
    def test_loss_with_sample_weight_in_layer_call(self):
        class MyLayer(layers.Layer):
            def __init__(self):
                super(MyLayer, self).__init__()
                self.bias = testing_utils.Bias()

            def call(self, inputs):
                out = self.bias(inputs[0])
                self.add_loss(MAE()(inputs[1], out, inputs[2]))
                self.add_loss(
                    math_ops.reduce_mean(inputs[2] * mae(inputs[1], out)))
                return out

        inputs = Input(shape=(1, ))
        targets = Input(shape=(1, ))
        sw = Input(shape=(1, ))

        outputs = MyLayer()([inputs, targets, sw])
        model = Model([inputs, targets, sw], outputs)
        model.predict([self.x, self.y, self.w])
        model.compile(optimizer_v2.gradient_descent.SGD(0.05),
                      run_eagerly=testing_utils.should_run_eagerly(),
                      experimental_run_tf_function=testing_utils.
                      should_run_tf_function())

        history = model.fit([self.x, self.y, self.w], batch_size=3, epochs=5)
        self.assertAllClose(history.history['loss'], [2., 1.8, 1.6, 1.4, 1.2],
                            1e-3)

        output = model.evaluate([self.x, self.y, self.w])
        self.assertAlmostEqual(output, 1.0, 3)

        output = model.test_on_batch([self.x, self.y, self.w])
        self.assertAlmostEqual(output, 1.0, 3)
Esempio n. 2
0
def test_distrib():
    pathModel = "../models/noSRMEndAno_200.h5"

    encoder = ev.encoder()
    decoder = ev.decoder()
    model = ev.srmAno(encoder, decoder)
    path = "./img_test/{}.jpg".format(1)
    img = cv2.imread(path, 1)
    img = img[..., ::-1]
    img = img.astype('float32') / 255.
    model.predict(np.array([img[0:32, 0:32]]))

    model.load_weights(pathModel)

    data = np.load("./data_to_load/splicedBorderAndOri.npy")
    mask = np.load("./data_to_load/maskSplicedBorderAndOri.npy")

    oriData, tampData = [], []
    countOri, countTamp = 0, 0
    for k, msk in enumerate(mask):
        if countTamp < 5:
            if np.sum(msk) > 1:
                tampData.append(data[k])
        else:
            if np.sum(msk) == 0:
                if countOri < 5:
                    oriData.append(data[k])
        if countTamp > 5 and countOri > 5:
            break
    model_x = Model(inputs=model.encoder.layers[0].input,
                    outputs=model.encoder.layers[7].output)
    model_tmp = model.encoder
    model_x_hat = Model(inputs=model.decoder.layers[0].input,
                        outputs=model.decoder.layers[2].output)

    oriData = np.array(oriData)
    tampData = np.array(tampData)

    ori_x = model_x.predict(oriData)
    tamp_x = model_x.predict(tampData)

    tmp_ori_x = model_tmp.predict(oriData)
    tmp_tamp_x = model_tmp.predict(tampData)

    ori_x_hat = model_x_hat.predict(tmp_ori_x)
    tamp_x_hat = model_x_hat.predict(tmp_tamp_x)

    np.save("./ori_x.npy", ori_x)
    np.save("./tamp_x.npy", tamp_x)

    np.save("./ori_x_hat.npy", ori_x_hat)
    np.save("./tamp_x_hat.npy", tamp_x_hat)

    return None
Esempio n. 3
0
class NN:
    """
    The NN class wraps a keras Sequential model to reduce the interface methods
    Notice:
        Difference to dqn is just the setter and getter methods for the weights
    """
    def __init__(self, env, atoms, alpha: float = 0.001, decay: float = 0.0001):
        """
        We initialize our functional model, therefore we need Input Shape and Output Shape
        :param env:
        :param alpha:
        :param decay:
        """
        self.alpha = alpha
        self.decay = decay
        self.model = None
        self.atoms = atoms
        # new to D-DDQN
        self.init_model(env.observation_space.shape[0], env.action_space.n)

    def init_model(self, input_shape: int, n_actions: int):
        """
        Initializing our keras sequential model
        :return: initialized model
        """
        input = Input(shape=(input_shape,))
        h1 = Dense(64, activation='relu')(input)
        h2 = Dense(64, activation='relu')(h1)
        outputs = []
        for _ in range(n_actions):
            outputs.append(Dense(self.atoms, activation='softmax')(h2))
        self.model = Model(input, outputs)

    def predict(self, *args, **kwargs):
        """
        By wrapping the keras predict method we can handle our net as a standalone object
        :param args: interface to keras.model.predict
        :return: prediction
        """
        return self.model.predict(*args, **kwargs)

    def fit(self, *args, **kwargs):
        """
        By wrapping the keras fit method we can handle our net as a standalone object
        :param args: interface to keras.model.fit
        :return: history object
        """
        return self.model.fit(*args, **kwargs)

    def get_weights(self):
        """
        Passing the arguments to keras get_weights
        """
        return self.model.get_weights()

    def set_weights(self, *args, **kwargs):
        """
        Passing the arguments to keras set_weights
        """
        self.model.set_weights(*args, *kwargs)
class Agent(object):
    def __init__(self, name='model', input_num=None, output_num=None):
        """A learning agent that uses tensorflow to create a neural network"""
        assert input_num is not None
        assert output_num is not None
        self.input_num = input_num
        self.output_num = output_num
        self._build_net()

    def _build_net(self):
        """Construct the neural network"""

        # Change the network structure here
        S = Input(shape=[self.input_num])
        h0 = Dense(300, activation="sigmoid")(S)
        h1 = Dense(600, activation="sigmoid")(h0)
        h2 = Dense(29, activation="sigmoid")(h1)
        V = Dense(self.output_num, activation="sigmoid")(h2)
        self.model = Model(inputs=S, outputs=V)
        self.model.compile(optimizer="adam", loss='mse')

    def train(self, x, y, n_epoch=100, batch=32):
        """Train the network"""
        self.model.fit(x=x, y=y, epochs=n_epoch, batch_size=batch)

    def predict(self, x):
        """Input values to the neural network and return the result"""
        a = self.model.predict(x)
        return a
 def get_embedded_input(model, encoded_text):
     """
     Get embedding layer output from a CNN model as the input for CNN_DCNN model
     """
     embedding_layer_model = Model(
         inputs=model.input,
         outputs=model.get_layer('word_embedding').output)
     return embedding_layer_model.predict(encoded_text)
Esempio n. 6
0
def prepare_image_data(paths):
    images_root = paths['images_root']
    captions_path = paths['train_captions_path']
    output_path = paths['image_features_path']

    if os.path.isfile(output_path):
        print('Image prep: Output file already exists, doing nothing.')
        return

    # 'avg_pool' is the final layer in InceptionV3 before 'predictions'. That is the
    # data used by VETE.
    # NOTE(laser): This will download InceptionV3 and depends on pillow and h5py
    base_model = inception_v3.InceptionV3(weights='imagenet', include_top=True)
    model = Model(inputs=base_model.input,
                  outputs=base_model.get_layer('avg_pool').output)

    with open(captions_path) as f:
        image_metadata = json.load(f)
    path = os.path.join(images_root, image_metadata['images'][0]['file_name'])

    chunk_size = 512
    image_ids = []
    result_list = []

    # OPTIMIZE(laser): In theory, image loading here is super slow. We're doing at
    # least 2-3 times the number of copies we need. In practice, this only needs to
    # run once over night.
    chunk_idx = 0
    for start in range(0, len(image_metadata['images']), chunk_size):
        image_count = 0
        image_list = []
        for image_entry in image_metadata['images'][start:start + chunk_size]:
            path = os.path.join(images_root, image_entry['file_name'])

            # NOTE(laser): Paper mentions rescaling to 300x300 but default arguments
            # in InceptionV3 docs say 299x299. Using that instead.
            img = image.load_img(path, target_size=(299, 299))
            x = image.img_to_array(img)
            image_ids.append(image_entry['id'])
            image_list.append(x)
            image_count += 1
            if image_count == chunk_size:
                chunk_idx += 1
                print('Loaded %s images (chunk %d)' %
                      (chunk_size * chunk_idx, chunk_idx - 1))
                break

        data = concat_np_list(image_list)
        data = inception_v3.preprocess_input(data)
        result = model.predict(data)
        result_list.append(result)
        print('Processed %s images (chunk %d)' %
              (chunk_size * chunk_idx, chunk_idx - 1))

    final_result = np.concatenate(result_list)
    final_result = np.insert(final_result, 0, np.array(image_ids), axis=1)
    final_result = np.sort(final_result, axis=0)
    np.save(output_path, final_result)
Esempio n. 7
0
 def output_test(self, data):
     layer_outputs = [layer.output for layer in self.predict_model.layers]
     activation_model = Model(inputs=self.predict_model.input,
                              outputs=layer_outputs)
     layer_name = [layer.name for layer in self.predict_model.layers]
     ans = activation_model.predict(data)
     # for pair in zip(layer_name, ans):
     # print(pair)
     return {name: value for name, value in zip(layer_name, ans)}
Esempio n. 8
0
def output_model_features(model, data, df):
    extraction_model = Model(inputs=model.input,
                             outputs=model.get_layer('features').output)

    for partition in ['train', 'devel', 'test']:
        features = extraction_model.predict(data[partition])
        write_agg_features_to_csv(df[partition], features, partition,
                                  feature_path + '_nn')
    return True
Esempio n. 9
0
def test_predict(data, wv_size, code_size):
    sess = tf.Session()

    with sess.as_default():
        model, h, input = get_model(wv_size, code_size)
        model.load_weights("exp_binary_code.h5")

        nm = Model(inputs=[input], outputs=[h])
        result = nm.predict(data)

    return result
Esempio n. 10
0
def perf_test_RASTAweights():
    """
    Test the performance of the RASTA weights provide by Lecoultre et al.
    """
    dataset = 'RASTA'
    sess = tf.Session()
    set_session(sess)
    tf.keras.backend.set_image_data_format('channels_last')
    
    base_model = resnet_trained(20)
    predictions = Dense(25, activation='softmax')(base_model.output)
    net_finetuned = Model(inputs=base_model.input, outputs=predictions)
    #net_finetuned = custom_resnet() # Ce model a 87 layers 
    
    path_to_model = os.path.join(os.sep,'media','gonthier','HDD2','output_exp','rasta_models','resnet_2017_7_31-19_9_45','model.h5')
    #ce model a 107 layers
    constrNet = 'LResNet50' # For Lecoutre ResNet50 version
    model_name = 'Lecoutre2017'
    input_name_lucid = 'input_1'
    
    net_finetuned.load_weights(path_to_model) # ,by_name=True
    net_finetuned.build((224,224,3))
    print(net_finetuned.summary())
    print(net_finetuned.predict(np.random.rand(1,224,224,3)))
    
    item_name,path_to_img,default_path_imdb,classes,ext,num_classes,str_val,df_label,\
            path_data,Not_on_NicolasPC = get_database(dataset)
    
    sLength = len(df_label[item_name])
    classes_vectors =  df_label[classes].values
    df_label_test = df_label[df_label['set']=='test']
    y_test = classes_vectors[df_label['set']=='test',:]
    
    cropCenter = False
    randomCrop = False
    imSize = 224
    predictions = predictionFT_net(net_finetuned,df_test=df_label_test,x_col=item_name,\
                                           y_col=classes,path_im=path_to_img,Net=constrNet,\
                                           cropCenter=cropCenter,randomCrop=randomCrop,\
                                           imSize=imSize)
    with sess.as_default():
        metrics = evaluationScoreRASTA(y_test,predictions) 
    top_k_accs,AP_per_class,P_per_class,R_per_class,P20_per_class,F1_per_class,acc_per_class= metrics

    for k,top_k_acc in zip([1,3,5],top_k_accs):
        print('Top-{0} accuracy : {1:.2f}%'.format(k,top_k_acc*100))
Esempio n. 11
0
    def update_weights_by_model(self, model: Model, part=0.01):
        print('Start update_weights_by_model')
        train_examples_count = 0
        for i in range(len(self.train)):
            train_examples_count += len(self.train[i])

        batch_size_saved = self.train_batch_size
        self.train_batch_size = int(train_examples_count * part)

        (t_images_1, t_images_2, t_results, indexes) = self.get_batch_train()

        self.update_weights(indexes, model.predict(x=[t_images_1, t_images_2]),
                            t_results)

        self.train_batch_size = batch_size_saved
        self.init_weight_normalize()
        print('Finish update_weights_by_model')
Esempio n. 12
0
def model_predict(model: Model, image: np.ndarray, should_turn_left: bool,
                  should_turn_right: bool):
    # image = obs["image"]  # 160x120px
    image = cv2.rotate(image, cv2.ROTATE_90_CLOCKWISE)
    image = cv2.flip(image, 1)
    image = image[36:, :]  # 160x84px

    turns_input = model.input_shape[1][0][2]
    if turns_input == 2:
        should_turns = [
            1 if should_turn_left else 0, 1 if should_turn_right else 0
        ]
    else:
        should_turns = -1 if should_turn_left else 1 if should_turn_right else 0
    to_pred = [[np.array([[image]]), np.array([[should_turns]])]]

    pred = model.predict(to_pred)[0] / 20
    return pred
Esempio n. 13
0
def predict_from_model(model: Model, input_text: str, json_conf: Dict,
                       char2int_encoder: Dict):
    """
    Make one prediction from encoded input with model

    Args:
        model: model predicting the next character
        input_text: input text for the model to sequence
        json_conf: dict of json config with model training parameters
        char2int_encoder: dict of char mapping to int used to train the model

    Returns:
        model softmax prediction as array
    """
    input_encoded = encode_sequences(
        [input_text[-json_conf.get("sequence_length"):]], char2int_encoder)
    input_padded = pad_sequence(input_encoded, json_conf)
    return model.predict(input_padded)
Esempio n. 14
0
def predication(model: keras.Model, frame: pd.DataFrame):
    pd_frame = copy.deepcopy(frame)
    pd_frame['value'] = None
    frame_value = frame.values[0]
    year, month, day = int(frame_value[0]), int(frame_value[1]), int(frame_value[2])
    target_day = datetime(year=year, month=month, day=day) + timedelta(days=1)
    pd_frame['year'] = target_day.year
    pd_frame['month'] = target_day.month
    pd_frame['day'] = target_day.day
    frame = frame.append(pd_frame)[['month', 'day', 'hour', 'minute']]
    predicate_values = []
    for start, end in zip(range(96), range(96, len(frame))):
        sample = np.expand_dims(frame[start:end].values, axis=0)
        predicate_value = model.predict(sample)[0][0]

        predicate_values.append(predicate_value)
        break
    return predicate_values
Esempio n. 15
0
def temporal_convolutional_network(data, settings):
    """Creates a Temporal Convolutional Network model (TCN) and predictions.

        Args:
            data: pandas.DataFrame.
            settings: Dictionary object containing settings parameters.
        Returns:
            A dictionary containing the TCN model and predictions.
        """

    #  TRAIN DATA GENERATOR
    train_generator = create_generator(data['train'],
                                       settings['morph'],
                                       shuffle=True)
    #  TRAIN DATA GENERATOR
    test_generator = create_generator(data['test'],
                                      settings['morph'],
                                      shuffle=False)

    #  INSTANTIATE KERAS TENSOR INPUT WITH TIMESERIESGENEREATOR SHAPE
    model_input = Input(batch_shape=train_generator[0][0].shape)

    #  INSTANTIATE MODEL LAYERS
    model_output = add_tcn_layers(model_input, settings)

    #  INSTANTIATE MODEL AND ASSIGN INPUT AND OUTPUT
    model = Model(inputs=[model_input], outputs=[model_output])

    # COMPILE THE MODEL
    model.compile(optimizer=settings['optimizer'], loss=settings['loss'])

    #  PRINT MODEL STATS
    tcn_full_summary(model, expand_residual_blocks=False)

    #  TRAIN THE MODEL WITH VALIDATION
    model.fit_generator(train_generator,
                        steps_per_epoch=len(train_generator),
                        epochs=settings['epochs'],
                        verbose=0)

    #  PREDICT USING TEST DATA
    predictions = model.predict(test_generator)

    return {'model': model, 'predictions': predictions}
Esempio n. 16
0
def evaluate_f1(model: keras.Model,
                vocab: Vocabulary,
                input_method_body_subtokens: np.ndarray,
                target_method_names: np.ndarray,
                hyperparameters: Dict[str, any],
                visualise_prediction=True):
    padding_id = vocab.get_id_or_unk(vocab.get_pad())
    begin_of_sentence_id = vocab.get_id_or_unk(SENTENCE_START_TOKEN)
    end_of_sentence_id = vocab.get_id_or_unk(SENTENCE_END_TOKEN)

    if input_method_body_subtokens.ndim != 3:
        # model prediction expects 3 dimensions, a single input won't have the batch dimension, manually add it
        input_method_body_subtokens = np.expand_dims(
            input_method_body_subtokens, 0)

    predictions = model.predict(input_method_body_subtokens, batch_size=1)

    best_predictions, best_predictions_probs = beam_search(
        predictions,
        padding_id,
        begin_of_sentence_id,
        end_of_sentence_id,
        hyperparameters['beam_width'],
        hyperparameters['beam_top_paths'],
    )
    f1_evaluation = _evaluate_f1(best_predictions, best_predictions_probs,
                                 vocab, target_method_names)
    if visualise_prediction:
        max_results = 10
        visualised_input = visualise_beam_predictions_to_targets(
            vocab, best_predictions[:max_results],
            best_predictions_probs[:max_results],
            input_method_body_subtokens[:max_results],
            target_method_names[:max_results])

        # return best_predictions, best_predictions_probs
        return f1_evaluation, visualised_input
    return f1_evaluation
def main(arg):

    directory = Path('./saved_predictions/')
    directory.mkdir(exist_ok=True)
    directory = Path('./saved_models/')
    directory.mkdir(exist_ok=True)
    directory = Path('./training_checkpoints/')
    directory.mkdir(exist_ok=True)
    input_yx_size = tuple(args.input_yx_size)
    batch_size = args.batch_size
    epochs = args.epochs
    learning_rate = args.learning_rate
    num_test_samples = args.num_test_samples
    save_weights = args.save_weights
    every = args.every
    num_samples = args.num_samples
    save_train_prediction = args.save_train_prediction
    save_test_prediction = args.save_test_prediction
    verbose = args.verbose
    validation_ratio = args.validation_ratio
    y_axis_len, x_axis_len = input_yx_size
    decay = args.decay
    decay = args.decay
    load_weights = args.load_weights
    y_axis_len, x_axis_len = input_yx_size
    num_points = y_axis_len * x_axis_len
    is_flat_channel_in = args.is_flat_channel_in
    input_points = Input(shape=(num_points, 4))

    x = input_points
    x = Convolution1D(64, 1, activation='relu', input_shape=(num_points, 4))(x)
    x = BatchNormalization()(x)
    x = Convolution1D(128, 1, activation='relu')(x)
    x = BatchNormalization()(x)
    x = Convolution1D(512, 1, activation='relu')(x)
    x = BatchNormalization()(x)
    x = MaxPooling1D(pool_size=num_points)(x)
    x = Dense(512, activation='relu')(x)
    x = BatchNormalization()(x)
    x = Dense(256, activation='relu')(x)
    x = BatchNormalization()(x)
    x = Dense(16,
              weights=[
                  np.zeros([256, 16]),
                  np.array([1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
                            1]).astype(np.float32)
              ])(x)
    input_T = Reshape((4, 4))(x)

    # forward net
    g = Lambda(mat_mul, arguments={'B': input_T})(input_points)
    g = Convolution1D(64, 1, input_shape=(num_points, 3), activation='relu')(g)
    g = BatchNormalization()(g)
    g = Convolution1D(64, 1, input_shape=(num_points, 3), activation='relu')(g)
    g = BatchNormalization()(g)

    # feature transformation net
    f = Convolution1D(64, 1, activation='relu')(g)
    f = BatchNormalization()(f)
    f = Convolution1D(128, 1, activation='relu')(f)
    f = BatchNormalization()(f)
    f = Convolution1D(128, 1, activation='relu')(f)
    f = BatchNormalization()(f)
    f = MaxPooling1D(pool_size=num_points)(f)
    f = Dense(512, activation='relu')(f)
    f = BatchNormalization()(f)
    f = Dense(256, activation='relu')(f)
    f = BatchNormalization()(f)
    f = Dense(64 * 64,
              weights=[
                  np.zeros([256, 64 * 64]),
                  np.eye(64).flatten().astype(np.float32)
              ])(f)
    feature_T = Reshape((64, 64))(f)

    # forward net
    g = Lambda(mat_mul, arguments={'B': feature_T})(g)
    seg_part1 = g
    g = Convolution1D(64, 1, activation='relu')(g)
    g = BatchNormalization()(g)
    g = Convolution1D(32, 1, activation='relu')(g)
    g = BatchNormalization()(g)
    g = Convolution1D(32, 1, activation='relu')(g)
    g = BatchNormalization()(g)

    # global_feature
    global_feature = MaxPooling1D(pool_size=num_points)(g)
    global_feature = Lambda(exp_dim, arguments={'num_points':
                                                num_points})(global_feature)

    # point_net_seg
    c = concatenate([seg_part1, global_feature])
    """ c = Convolution1D(512, 1, activation='relu')(c)
    c = BatchNormalization()(c)
    c = Convolution1D(256, 1, activation='relu')(c)
    c = BatchNormalization()(c)
    c = Convolution1D(128, 1, activation='relu')(c)
    c = BatchNormalization()(c)
    c = Convolution1D(128, 1, activation='relu')(c)
    c = BatchNormalization()(c) """
    c = Convolution1D(256, 1, activation='relu')(c)
    c = BatchNormalization()(c)
    c = Convolution1D(128, 4, activation='relu', strides=4)(c)
    c = BatchNormalization()(c)
    c = Convolution1D(128, 4, activation='relu', strides=4)(c)
    c = BatchNormalization()(c)
    c = Convolution1D(128, 4, activation='relu', strides=4)(c)
    c = BatchNormalization()(c)
    c = Convolution1D(64, 4, activation='relu', strides=4)(c)
    c = BatchNormalization()(c)
    c = Convolution1D(64, 4, activation='relu', strides=4)(c)
    c = BatchNormalization()(c)
    c = Convolution1D(32, 1, activation='relu')(c)
    c = BatchNormalization()(c)
    """ c = Convolution1D(128, 4, activation='relu',strides=4)(c)
    c = Convolution1D(64, 4, activation='relu',strides=4)(c)
    c = Convolution1D(32, 4, activation='relu',strides=4)(c)
    c = Convolution1D(16, 1, activation='relu')(c)
    c = Convolution1D(1, 1, activation='relu')(c) """
    #c = tf.keras.backend.squeeze(c,3);
    c = CuDNNLSTM(64, return_sequences=False)(c)
    #c =CuDNNLSTM(784, return_sequences=False))
    #c =CuDNNLSTM(256, return_sequences=False))

    #c = Reshape([16,16,1])(c)
    c = Reshape([8, 8, 1])(c)
    c = Conv2DTranspose(8, (3, 3),
                        padding="same",
                        activation="relu",
                        strides=(2, 2))(c)
    c = Conv2DTranspose(8, (3, 3), padding="valid", activation="relu")(c)
    #c =Dropout(0.4))
    c = tf.keras.layers.BatchNormalization()(c)
    c = Conv2DTranspose(16, (3, 3), padding="valid", activation="relu")(c)
    #c =Dropout(0.4))
    c = tf.keras.layers.BatchNormalization()(c)
    c = Conv2DTranspose(32, (3, 3), padding="valid", activation="relu")(c)
    #c =Dropout(0.4))
    c = tf.keras.layers.BatchNormalization()(c)
    c = Conv2DTranspose(32, (3, 3), padding="valid", activation="relu")(c)
    #c =Dropout(0.4))
    c = tf.keras.layers.BatchNormalization()(c)
    c = Conv2DTranspose(32, (3, 3), padding="valid", activation="relu")(c)
    #c =Dropout(0.4))
    c = tf.keras.layers.BatchNormalization()(c)
    c = Conv2DTranspose(64, (3, 3), padding="valid", activation="relu")(c)
    #c =Dropout(0.4))
    c = tf.keras.layers.BatchNormalization()(c)
    c = Conv2DTranspose(64, (3, 3), padding="valid", activation="relu")(c)
    #c =Dropout(0.4))
    c = tf.keras.layers.BatchNormalization()(c)

    #c =Dropout(0.4))

    c = Conv2DTranspose(128, (3, 3),
                        padding="same",
                        activation="relu",
                        strides=(2, 2))(c)
    c = tf.keras.layers.BatchNormalization()(c)

    c = Conv2DTranspose(128, (3, 3), padding="valid", activation="relu")(c)

    #c =Dropout(0.4))
    c = tf.keras.layers.BatchNormalization()(c)
    c = Conv2DTranspose(128, (3, 3),
                        padding="same",
                        activation="relu",
                        strides=(2, 2))(c)
    c = tf.keras.layers.BatchNormalization()(c)

    c = Conv2DTranspose(128, (3, 3), padding="valid", activation="relu")(c)
    c = tf.keras.layers.BatchNormalization()(c)

    #c =Dropout(0.4))
    #c =tf.keras.layers.BatchNormalization())
    c = Conv2DTranspose(64, (3, 3), padding="same", strides=(4, 2))(c)
    c = tf.keras.layers.BatchNormalization()(c)

    c = Conv2DTranspose(32, (3, 3), padding="valid", activation="relu")(c)
    c = tf.keras.layers.BatchNormalization()(c)

    c = Conv2DTranspose(32, (3, 3), padding="valid", activation="relu")(c)
    c = tf.keras.layers.BatchNormalization()(c)

    #c =Dropout(0.4))
    c = Conv2DTranspose(32, (3, 3),
                        padding="same",
                        activation="relu",
                        strides=(1, 1))(c)
    c = tf.keras.layers.BatchNormalization()(c)

    c = Conv2DTranspose(32, (3, 1), padding="valid", activation="relu")(c)
    c = tf.keras.layers.BatchNormalization()(c)

    c = Conv2DTranspose(32, (3, 1), padding="valid", activation="relu")(c)
    c = tf.keras.layers.BatchNormalization()(c)
    c = Conv2DTranspose(16, (1, 1), padding="valid", activation="relu")(c)
    c = tf.keras.layers.BatchNormalization()(c)
    c = Conv2DTranspose(8, (1, 1), padding="valid", activation="relu")(c)
    c = tf.keras.layers.BatchNormalization()(c)

    c = Conv2DTranspose(1, (1, 1), padding="valid")(c)
    """ c =Conv2DTranspose(4, (1,1),padding="same",activation="relu"))
    c =Conv2DTranspose(2, (1,1),padding="same",activation="relu"))
    #c =Dropout(0.4))
    c =Conv2DTranspose(1, (1,1),padding="same")) """
    prediction = tf.keras.layers.Reshape([512, 256])(c)
    """ c1 ,c2  = tf.split(c,[256,256],axis=1,name="split")
    complexNum = tf.dtypes.complex(
        c1,
        c2,
        name=None
    )

    complexNum =tf.signal.ifft2d(
        complexNum,
        name="IFFT"
    )
    real = tf.math.real(complexNum)
    imag = tf.math.imag(complexNum)

    con = concatenate([real,imag])

    prediction  =tf.keras.layers.Reshape([ 512, 256])(con)
    """
    # define model
    model = Model(inputs=input_points, outputs=prediction)
    opt = tf.keras.optimizers.Adam(lr=learning_rate, decay=decay)

    loss = tf.keras.losses.MeanSquaredError()
    mertric = ['mse']
    if args.loss is "MAE":
        loss = tf.keras.losses.MeanAbsoluteError()
        mertric = ['mae']

    model.compile(
        loss=loss,
        optimizer=opt,
        metrics=mertric,
    )

    model.summary()
    if load_weights:
        model.load_weights('./training_checkpoints/cp-best_loss.ckpt')

    #edit data_loader.py if you want to play with data
    input_ks, ground_truth = load_data(num_samples,
                                       is_flat_channel_in=is_flat_channel_in)

    input_ks = input_ks / np.max(input_ks)

    checkpoint_path = "./training_checkpoints/cp-{epoch:04d}.ckpt"
    checkpoint_dir = os.path.dirname(checkpoint_path)

    # Create checkpoint callback
    #do you want to save the model's wieghts? if so set this varaible to true

    cp_callback = []

    NAME = "NUFFT_NET"

    tensorboard = TensorBoard(log_dir="logs/{}".format(NAME))
    cp_callback.append(tensorboard)
    if save_weights:
        cp_callback.append(
            tf.keras.callbacks.ModelCheckpoint(checkpoint_dir,
                                               save_weights_only=True,
                                               verbose=verbose,
                                               period=every))

    if args.is_train:
        model.fit(input_ks,
                  ground_truth,
                  batch_size=batch_size,
                  epochs=epochs,
                  validation_split=validation_ratio,
                  callbacks=cp_callback)

    if args.name_model is not "":
        model.save('./saved_mdoels/' + args.name_model)
    dict_name = './saved_predictions/'
    #return to image size
    x_axis_len = int(x_axis_len / 4)
    np.random.seed(int(time()))

    if save_train_prediction <= num_samples:
        rand_ix = np.random.randint(0, num_samples - 1, save_train_prediction)
        #kspace = np.zeros((save_train_prediction,
        #y_axis_len,input_ks[rand_ix].shape[1]))
        kspace = input_ks[rand_ix]
        if args.save_input:
            np.save("./saved_predictions/inputs.npy", input_ks[rand_ix])
        ground_truth = ground_truth[rand_ix]
        preds = model.predict(kspace, batch_size=save_train_prediction)
        for i in range(save_train_prediction):

            output = np.reshape(preds[i], (y_axis_len * 2, x_axis_len))
            output = output * 255
            output[np.newaxis, ...]
            output_gt = ground_truth[i]
            output_gt[np.newaxis, ...]
            output = np.concatenate([output, output_gt], axis=0)
            np.save(dict_name + 'prediction%d.npy' % (i + 1), output)

        input_ks, ground_truth = load_data(
            num_test_samples, 'test', is_flat_channel_in=is_flat_channel_in)

        input_ks = input_ks / np.max(input_ks)
    if args.is_eval:
        model.evaluate(input_ks,
                       ground_truth,
                       batch_size,
                       verbose,
                       callbacks=cp_callback)

    if save_test_prediction <= num_test_samples:
        rand_ix = np.random.randint(0, num_test_samples - 1,
                                    save_test_prediction)
        kspace = input_ks[rand_ix]
        if args.save_input:
            np.save("./saved_predictions/test_inputs.npy", input_ks[rand_ix])
        ground_truth = ground_truth[rand_ix]
        preds = model.predict(kspace, batch_size=save_test_prediction)
        for i in range(save_test_prediction):

            output = np.reshape(preds[i], (y_axis_len * 2, x_axis_len))
            output = output * 255
            output[np.newaxis, ...]
            output_gt = ground_truth[i]
            output_gt[np.newaxis, ...]
            output = np.concatenate([output, output_gt], axis=0)
            np.save(dict_name + 'test_prediction%d.npy' % (i + 1), output)
Esempio n. 18
0
class Char_Inference():

    def __init__(self, word_level, architecture, latent_dim):

        model_name = "architecture" + str(architecture)
        if word_level:
            model_path = settings.TRAINED_MODELS_PATH + model_name + "/" + model_name + "word.h5"
        else:
            model_path = settings.TRAINED_MODELS_PATH + model_name + "/" + model_name + "char.h5"

        print(model_path)

        self.model = models.load_model(model_path)
        self.encoder_states = None
        self.latent_dim = latent_dim
        general_info = load_pickle_data(settings.DATASET_CHAR_INFERENCE_INFORMATION_PATH)
        settings.MFCC_FEATURES_LENGTH = general_info[0]
        settings.CHARACTER_SET = general_info[2]
        self.encoder_model = None
        self.decoder_model = None


        if architecture == 6:
            self._get_encoder_decoder_model_baseline()
        else:
            self._get_encoder_decoder_model_cnn()

    def predict_sequence_test(self, audio_input):
        char_to_int = convert_to_int(sorted(settings.CHARACTER_SET))
        int_to_char = convert_int_to_char(char_to_int)

        t_force = "\tmsA' Alxyr >wlA hw Almwqf tm tSHyHh\n"
        encoded_transcript = []
        for index, character in enumerate(t_force):
            encoded_character = [0] * len(settings.CHARACTER_SET)
            position = char_to_int[character]
            encoded_character[position] = 1
            encoded_transcript.append(encoded_character)

        decoder_input = np.array([encoded_transcript])
        print(decoder_input.shape)

        output = self.model.predict([audio_input, decoder_input])
        print(output.shape)
        sentence = ""
        output = output[0]
        for character in output:
            position = np.argmax(character)
            character = int_to_char[position]
            sentence+=character

        print(sentence)

    def _get_encoder_decoder_model_cnn(self):
        # Getting encoder model
        encoder_inputs = self.model.get_layer("encoder_input").input

        cnn_model = self.model.get_layer("sequential")
        encoder_inputs_cnn = cnn_model(encoder_inputs)

        encoder_gru = self.model.get_layer("encoder_gru_layer")
        encoder_output, h = encoder_gru(encoder_inputs_cnn)
        self.encoder_states = h

        self.encoder_model = Model(encoder_inputs, self.encoder_states)
        self.encoder_model.summary()
        # Getting decoder model

        decoder_inputs = self.model.get_layer("decoder_input").input

        decoder_gru1_layer = self.model.get_layer("decoder_gru1_layer")
        decoder_gru2_layer = self.model.get_layer("decoder_gru2_layer")
        decoder_gru3_layer = self.model.get_layer("decoder_gru3_layer")

        decoder_dropout = self.model.get_layer("decoder_dropout")
        decoder_dense_layer = self.model.get_layer("decoder_dense")

        decoder_state_input_h = Input(shape=(self.latent_dim,))
        decoder_states_inputs = [decoder_state_input_h]

        decoder_gru1, state_h = decoder_gru1_layer(decoder_inputs, initial_state=decoder_states_inputs)
        decoder_gru2 = decoder_gru2_layer(decoder_gru1)
        decoder_output = decoder_gru3_layer(decoder_gru2)

        decoder_states = [state_h]

        # getting dense layers as outputs
        decoder_output  = decoder_dropout(decoder_output)
        decoder_output = decoder_dense_layer(decoder_output)

        self.decoder_model = Model(
            [decoder_inputs] + decoder_states_inputs,
            [decoder_output] + decoder_states)

    def _get_encoder_decoder_model_baseline(self):

        # Getting encoder model
        encoder_inputs = self.model.get_layer("encoder_input").input
        encoder_gru = self.model.get_layer("encoder_gru_layer")
        encoder_output, h = encoder_gru(encoder_inputs)
        self.encoder_states = h

        self.encoder_model = Model(encoder_inputs, self.encoder_states)
        self.encoder_model.summary()
        # Getting decoder model

        decoder_inputs = self.model.get_layer("decoder_input").input

        decoder_gru1_layer = self.model.get_layer("decoder_gru1_layer")
        decoder_gru2_layer = self.model.get_layer("decoder_gru2_layer")
        decoder_gru3_layer = self.model.get_layer("decoder_gru3_layer")


        decoder_dense_layer = self.model.get_layer("decoder_dense")

        decoder_state_input_h = Input(shape=(self.latent_dim, ))
        decoder_states_inputs = [decoder_state_input_h]

        decoder_gru1, state_h = decoder_gru1_layer(decoder_inputs, initial_state=decoder_states_inputs)
        decoder_gru2 = decoder_gru2_layer(decoder_gru1)
        decoder_output = decoder_gru3_layer(decoder_gru2)
        decoder_states = [state_h]

        # getting dense layers as outputs

        decoder_output = decoder_dense_layer(decoder_output)

        self.decoder_model = Model(
            [decoder_inputs] + decoder_states_inputs,
            [decoder_output] + decoder_states)




    def decode_audio_sequence_character_based(self, audio_sequence):
        """
        Decodes audio sequence into a transcript using encoder_model and decoder_model generated from training
        :param audio_sequence: 2D numpy array
        :param encoder_model: Model
        :param decoder_model: Model
        :param character_set: Dict
        :return: String
        """
        # Getting converters
        char_to_int = convert_to_int(sorted(settings.CHARACTER_SET))
        int_to_char = convert_int_to_char(char_to_int)

        # Returns the encoded audio_sequence
        states_value = self.encoder_model.predict(audio_sequence)
        states_value = [states_value]
        print("ENCODER PREDICTION DONE")
        num_decoder_tokens = len(char_to_int)
        target_sequence = np.zeros((1, 1, num_decoder_tokens))

        # Populate the first character of target sequence with the start character.
        target_sequence[0, 0, char_to_int['\t']] = 1.
        print(target_sequence)
        stop_condition = False
        t_force = "\tmsA' Alxyr >wlA hw Almwqf tm tSHyHh\n"
        decoded_sentence = ''
        max_length = len(t_force)
        i = 0
        while not stop_condition:
            output_tokens, h = self.decoder_model.predict(
                [target_sequence] + states_value)
            states_value = [h]
            #print("DECODER PREDICTION DONE khobz")
            sampled_token_index = np.argmax(output_tokens[0, -1, :])
            sampled_char = int_to_char[sampled_token_index]
            #print(sampled_char)
            decoded_sentence += sampled_char

            if sampled_char == "\n" or len(decoded_sentence) > max_length :
                # End of transcription
                stop_condition = True
            else:
                # updating target sequence vector
                target_sequence = np.zeros((1, 1, num_decoder_tokens))
                target_sequence[0, 0, char_to_int[t_force[i]]] = 1
                i += 1

        print(decoded_sentence)
        return decoded_sentence
Esempio n. 19
0
def testDiversVaries():
    #tf.keras.backend.clear_session()
    #tf.reset_default_graph()
    #K.set_learning_phase(0)
    
    sess = tf.Session()
    #graph = tf.get_default_graph()
    #keras.backend.set_session(sess)
    # IMPORTANT: models have to be loaded AFTER SETTING THE SESSION for keras! 
    # Otherwise, their weights will be unavailable in the threads after the session there has been set
    set_session(sess)
    
    
    original_model = resnet_trained(n_retrain_layers=20)
     # Cela va charger un tf.keras model
    
    base_model = resnet_trained(20)
    predictions = Dense(25, activation='softmax')(base_model.output)
    net_finetuned = Model(inputs=base_model.input, outputs=predictions)
    
    net_finetuned.predict(np.random.rand(1,224,224,3))
    trainable_layers_name = []
    for original_layer in original_model.layers:
        if original_layer.trainable:
            trainable_layers_name += [original_layer.name]
    #C:\media\gonthier\HDD2\output_exp\rasta_models\resnet_2017_7_31-19_9_45
    
    path_to_model = os.path.join(os.sep,'media','gonthier','HDD2','output_exp','rasta_models','resnet_2017_7_31-19_9_45','model.h5')
    
    constrNet = 'LResNet50' # For Lecoutre ResNet50 version
    model_name = 'Lecoutre2017'
    input_name_lucid = 'input_1'
    
    tf.keras.backend.set_image_data_format('channels_last')
    
    net_finetuned.load_weights(path_to_model,by_name=True)
    net_finetuned.build((224,224,3))
    net_finetuned.summary()
    net_finetuned.predict(np.random.rand(1,224,224,3))
    #net_finetuned = keras.models.load_model(path_to_model,compile=True)
    #net_finetuned = load_model(path_to_model,compile=True)
    
    number_of_trainable_layer = 20 
    #
    #list_layer_index_to_print = []
    #for layer in model.layers:
    #    trainable_l = layer.trainable
    #    name_l = layer.name
    #    if trainable_l and 'res' in name_l:
    #        print(name_l,trainable_l)
    #        num_features = tf.shape(layer.bias).eval(session=sess)[0]
    #        list_layer_index_to_print += [name_l,np.arange(0,num_features)]
    #        
    #for layer in original_model.layers:
    #    print(layer)
    #    trainable_l = layer.trainable
    #    name_l = layer.name
    #    if trainable_l and 'res' in name_l:
    #        print(name_l,trainable_l)
    #        num_features = tf.shape(layer.bias).eval(session=sess)[0]
    #        list_layer_index_to_print += [name_l,np.arange(0,num_features)]
            
    #list_weights,list_name_layers = get_weights_and_name_layers_forPurekerasModel(original_model)
    list_weights,list_name_layers = CompNet_FT_lucidIm.get_weights_and_name_layers(original_model)
    
    dict_layers_relative_diff,dict_layers_argsort = CompNet_FT_lucidIm.get_gap_between_weights(list_name_layers,\
                                                                                    list_weights,net_finetuned)
    
    layer_considered_for_print_im = []
    for layer in net_finetuned.layers:
        trainable_l = layer.trainable
        name_l = layer.name
        print(name_l,trainable_l)
        if trainable_l and (name_l in trainable_layers_name):
            layer_considered_for_print_im += [name_l]
    num_top = 3
    list_layer_index_to_print_base_model = []
    list_layer_index_to_print = []
    #print(layer_considered_for_print_im)
    for key in dict_layers_argsort.keys():
        #print(key)
        if not(key in layer_considered_for_print_im):
            continue
        for k in range(num_top):
             topk = dict_layers_argsort[key][k]
             list_layer_index_to_print += [[key,topk]]
             list_layer_index_to_print_base_model += [[key,topk]]
    
    print('list_layer_index_to_print',list_layer_index_to_print)
    #dict_list_layer_index_to_print_base_model[model_name+suffix] = list_layer_index_to_print_base_model
    
    #dict_layers_relative_diff,dict_layers_argsort = CompNet_FT_lucidIm.get_gap_between_weights(list_name_layers,\
    #                                                    list_weights,model)
    
    # For the fine-tuned model !!!
    path_lucid_model = os.path.join(os.sep,'media','gonthier','HDD2','output_exp','Covdata','Lucid_model')
    path = path_lucid_model
    if path=='':
        os.makedirs('./model', exist_ok=True)
        path ='model'
    else:
        os.makedirs(path, exist_ok=True)
    
    frozen_graph = lucid_utils.freeze_session(sess,
                              output_names=[out.op.name for out in net_finetuned.outputs])
    
    name_pb = 'tf_graph_'+constrNet+model_name+'.pb'
    
    #nodes_tab = [n.name for n in tf.get_default_graph().as_graph_def().node]
    #print(nodes_tab)
    tf.io.write_graph(frozen_graph,logdir= path,name= name_pb, as_text=False)
    
    if platform.system()=='Windows': 
        output_path = os.path.join('CompModifModel',constrNet)
    else:
        output_path = os.path.join(os.sep,'media','gonthier','HDD2','output_exp','Covdata','CompModifModel',constrNet)
    pathlib.Path(output_path).mkdir(parents=True, exist_ok=True) 
    
    matplotlib.use('Agg')
    output_path_with_model = os.path.join(output_path,model_name)
    pathlib.Path(output_path_with_model).mkdir(parents=True, exist_ok=True)
    
#    global sess
#    global graph
#    with graph.as_default():
#        set_session(sess)
#        net_finetuned.predict(np.random.rand(1,224,224,3))
    net_finetuned.predict(np.random.rand(1,224,224,3))
    lucid_utils.print_images(model_path=path_lucid_model+'/'+name_pb,list_layer_index_to_print=list_layer_index_to_print\
             ,path_output=output_path_with_model,prexif_name=model_name,input_name=input_name_lucid,Net=constrNet)
    
    # For the original one !!! 
    original_model.predict(np.random.rand(1,224,224,3))
    #sess = keras.backend.get_session()
    #sess.run()
    frozen_graph = lucid_utils.freeze_session(sess,
                              output_names=[out.op.name for out in original_model.outputs])
    
    name_pb = 'tf_graph_'+constrNet+'PretrainedImageNet.pb'
    tf.io.write_graph(frozen_graph,logdir= path,name= name_pb, as_text=False)
    lucid_utils.print_images(model_path=path_lucid_model+'/'+name_pb,list_layer_index_to_print=list_layer_index_to_print\
         ,path_output=output_path_with_model,prexif_name=model_name,input_name=input_name_lucid,Net=constrNet)
def run_single_test(algorithm_def,
                    gen_train,
                    gen_val,
                    load_weights,
                    freeze_weights,
                    x_test,
                    y_test,
                    lr,
                    batch_size,
                    epochs,
                    epochs_warmup,
                    model_checkpoint,
                    scores,
                    loss,
                    metrics,
                    logging_path,
                    kwargs,
                    clipnorm=None,
                    clipvalue=None,
                    model_callback=None):
    print(metrics)
    print(loss)

    metrics = make_custom_metrics(metrics)
    loss = make_custom_loss(loss)

    if load_weights:
        enc_model = algorithm_def.get_finetuning_model(model_checkpoint)
    else:
        enc_model = algorithm_def.get_finetuning_model()

    pred_model = apply_prediction_model(
        input_shape=enc_model.outputs[0].shape[1:],
        algorithm_instance=algorithm_def,
        **kwargs)

    outputs = pred_model(enc_model.outputs)
    model = Model(inputs=enc_model.inputs[0], outputs=outputs)
    print_flat_summary(model)

    if epochs > 0:
        callbacks = [TerminateOnNaN()]

        logging_csv = False
        if logging_path is not None:
            logging_csv = True
            logging_path.parent.mkdir(exist_ok=True, parents=True)
            logger_normal = CSVLogger(str(logging_path), append=False)
            logger_after_warmup = LogCSVWithStart(
                str(logging_path), start_from_epoch=epochs_warmup, append=True)
        if freeze_weights or load_weights:
            enc_model.trainable = False

        if freeze_weights:
            print(("-" * 10) +
                  "LOADING weights, encoder model is completely frozen")
            if logging_csv:
                callbacks.append(logger_normal)
        elif load_weights:
            assert epochs_warmup < epochs, "warmup epochs must be smaller than epochs"

            print(("-" * 10) +
                  "LOADING weights, encoder model is trainable after warm-up")
            print(("-" * 5) + " encoder model is frozen")

            w_callbacks = list(callbacks)
            if logging_csv:
                w_callbacks.append(logger_normal)

            model.compile(optimizer=get_optimizer(clipnorm, clipvalue, lr),
                          loss=loss,
                          metrics=metrics)
            model.fit(
                x=gen_train,
                validation_data=gen_val,
                epochs=epochs_warmup,
                callbacks=w_callbacks,
            )
            epochs = epochs - epochs_warmup

            enc_model.trainable = True
            print(("-" * 5) + " encoder model unfrozen")

            if logging_csv:
                callbacks.append(logger_after_warmup)
        else:
            print(("-" * 10) +
                  "RANDOM weights, encoder model is fully trainable")
            if logging_csv:
                callbacks.append(logger_normal)

        # recompile model
        model.compile(optimizer=get_optimizer(clipnorm, clipvalue, lr),
                      loss=loss,
                      metrics=metrics)
        model.fit(x=gen_train,
                  validation_data=gen_val,
                  epochs=epochs,
                  callbacks=callbacks)

    model.compile(optimizer=get_optimizer(clipnorm, clipvalue, lr),
                  loss=loss,
                  metrics=metrics)
    y_pred = model.predict(x_test, batch_size=batch_size)
    scores_f = make_scores(y_test, y_pred, scores)

    if model_callback:
        model_callback(model)

    # cleanup
    del pred_model
    del enc_model
    del model

    algorithm_def.purge()
    K.clear_session()

    for i in range(15):
        gc.collect()

    for s in scores_f:
        print("{} score: {}".format(s[0], s[1]))

    return scores_f
from tensorflow import keras
import random

from tensorflow.python.keras import Model, Input

from Scripts.AELoader import display_reconstructed_images
from Scripts.aae.datagen import aae_load_images
from Scripts.aae.params import PARAMS

path = "all_outputs/my_vae_exp_261"
vae = keras.models.load_model(path, compile=False)
encoder = Model(vae.input, vae.get_layer("origin_encoder").output)
# encoder = keras.models.load_model("encoder_vae_exp_261", compile=False)
decoder_input = Input(shape=(PARAMS["z_dim"],))

decoder = Model(decoder_input, vae.get_layer("origin_decoder")(decoder_input))

encoder.save("encoder_vae_exp_261")
encoder.summary()
decoder.summary()

test = aae_load_images(image_size=64)
lst = random.sample(range(0, len(test) - 1), 50)
to_predict = test[lst]

encoded_images = encoder.predict(to_predict)
decoded_images = decoder.predict(encoded_images)
# decoded_images = vae.predict(to_predict)

display_reconstructed_images(to_predict, decoded_images, 64, 64)
class PolicyModel:
    def __init__(self, config: Config):
        self.config = config
        self.model: Model = None

    def load_model(self):
        try:
            self.model = load_model(str(self.model_file_path))
            logger.info(f"loading policy model success")
            return True
        except Exception:
            return False

    def save_model(self):
        logger.info(f"saving policy model")
        self.model_file_path.parent.mkdir(parents=True, exist_ok=True)
        self.model.save(str(self.model_file_path), include_optimizer=False)

    def build(self):
        logger.info(f"setup state model")
        in_state = Input((self.config.model.vae.latent_dim, ), name="in_state")
        in_keys = Input((1, ), name="in_keys")
        in_time = Input((1, ), name="in_time")
        in_actions = Input((self.config.policy_model.n_actions, ),
                           name="in_actions")
        in_rarity = Input((1, ), name="in_rarity")
        in_all = Concatenate(name="in_all")(
            [in_state, in_keys, in_time, in_actions, in_rarity])
        x = Dense(self.config.policy_model.hidden_size,
                  activation="tanh",
                  name="hidden",
                  kernel_regularizer=l2(0.0001))(in_all)
        out_actions = Dense(self.config.policy_model.n_actions,
                            activation="softmax",
                            name="parameters",
                            kernel_regularizer=l2(0.0001))(x)
        out_keep_rate = Dense(1, activation="sigmoid",
                              name="keep_rate")(in_all)
        self.model = Model([in_state, in_keys, in_time, in_actions, in_rarity],
                           [out_actions, out_keep_rate],
                           name="policy_model")

    def predict(self, state, keys, time_remain, in_actions, in_rarity):
        actions, kr = self.model.predict([
            np.expand_dims(state, axis=0),
            np.array([[keys]]),
            np.array([[time_remain]]),
            np.expand_dims(in_actions, axis=0),
            np.array([[in_rarity]]),
        ])
        return actions[0], kr[0]

    def get_parameters(self):
        return self.model.get_weights()

    def set_parameters(self, parameters):
        self.model.set_weights(parameters)

    def compile(self):
        self.model.compile(
            optimizer=Adam(lr=0.00001),
            loss=[kullback_leibler_divergence, mean_squared_error],
            loss_weights=[1., 0.])

    @property
    def model_file_path(self):
        return self.config.resource.model_dir / "policy_weights.h5"
Esempio n. 23
0
class jyHEDModelV2_2_SGD_GradientTape_L1(jyModelBase):
    def __init__(self):
        super(jyHEDModelV2_2_SGD_GradientTape_L1, self).__init__()
        self.__listLayerName = []
        self.__pVisualModel = None
        self.__bLoadModel = False
        self.__pTrainFW = tf.summary.create_file_writer(self._strLogPath +
                                                        '/train')
        self.__pValidFW = tf.summary.create_file_writer(self._strLogPath +
                                                        '/valid')
        self.__pMetricsFW = tf.summary.create_file_writer(self._strLogPath +
                                                          '/metrics')

    def structureModel(self):
        weightDecay = 0.00001
        Inputs = layers.Input(shape=self._inputShape,
                              batch_size=self._iBatchSize)
        Con1 = layers.Conv2D(64, (3, 3),
                             name='Con1',
                             activation='relu',
                             padding='SAME',
                             input_shape=self._inputShape,
                             strides=1,
                             kernel_regularizer=l2(weightDecay))(Inputs)
        Con2 = layers.Conv2D(64, (3, 3),
                             name='Con2',
                             activation='relu',
                             padding='SAME',
                             strides=1,
                             kernel_regularizer=l2(weightDecay))(Con1)
        Side1 = sideBranch(Con2, 1)
        MaxPooling1 = layers.MaxPooling2D((2, 2),
                                          name='MaxPooling1',
                                          strides=2,
                                          padding='SAME')(Con2)
        # outputs1
        Con3 = layers.Conv2D(128, (3, 3),
                             name='Con3',
                             activation='relu',
                             padding='SAME',
                             strides=1,
                             kernel_regularizer=l2(weightDecay))(MaxPooling1)
        Con4 = layers.Conv2D(128, (3, 3),
                             name='Con4',
                             activation='relu',
                             padding='SAME',
                             strides=1,
                             kernel_regularizer=l2(weightDecay))(Con3)
        Side2 = sideBranch(Con4, 2)
        MaxPooling2 = layers.MaxPooling2D((2, 2),
                                          name='MaxPooling2',
                                          strides=2,
                                          padding='SAME')(Con4)
        # outputs2
        Con5 = layers.Conv2D(256, (3, 3),
                             name='Con5',
                             activation='relu',
                             padding='SAME',
                             strides=1,
                             kernel_regularizer=l2(weightDecay))(MaxPooling2)
        Con6 = layers.Conv2D(256, (3, 3),
                             name='Con6',
                             activation='relu',
                             padding='SAME',
                             strides=1,
                             kernel_regularizer=l2(weightDecay))(Con5)
        Con7 = layers.Conv2D(256, (3, 3),
                             name='Con7',
                             activation='relu',
                             padding='SAME',
                             strides=1,
                             kernel_regularizer=l2(weightDecay))(Con6)
        Side3 = sideBranch(Con7, 4)
        MaxPooling3 = layers.MaxPooling2D((2, 2),
                                          name='MaxPooling3',
                                          strides=2,
                                          padding='SAME')(Con7)
        # outputs3
        Con8 = layers.Conv2D(512, (3, 3),
                             name='Con8',
                             activation='relu',
                             padding='SAME',
                             strides=1,
                             kernel_regularizer=l2(weightDecay))(MaxPooling3)
        Con9 = layers.Conv2D(512, (3, 3),
                             name='Con9',
                             activation='relu',
                             padding='SAME',
                             strides=1,
                             kernel_regularizer=l2(weightDecay))(Con8)
        Con10 = layers.Conv2D(512, (3, 3),
                              name='Con10',
                              activation='relu',
                              padding='SAME',
                              strides=1,
                              kernel_regularizer=l2(weightDecay))(Con9)
        Side4 = sideBranch(Con10, 8)
        MaxPooling4 = layers.MaxPooling2D((2, 2),
                                          name='MaxPooling4',
                                          strides=2,
                                          padding='SAME')(Con10)
        # outputs4
        Con11 = layers.Conv2D(512, (3, 3),
                              name='Con11',
                              activation='relu',
                              padding='SAME',
                              strides=1,
                              kernel_regularizer=l2(weightDecay))(MaxPooling4)
        Con12 = layers.Conv2D(512, (3, 3),
                              name='Con12',
                              activation='relu',
                              padding='SAME',
                              strides=1,
                              kernel_regularizer=l2(weightDecay))(Con11)
        Con13 = layers.Conv2D(512, (3, 3),
                              name='Con13',
                              activation='relu',
                              padding='SAME',
                              strides=1,
                              kernel_regularizer=l2(weightDecay))(Con12)
        Side5 = sideBranch(Con13, 16)
        Fuse = layers.Concatenate(axis=-1)([Side1, Side2, Side3, Side4, Side5])

        # learn fusion weight
        fuseInitWeight = initializers.constant(0.2)
        Fuse = layers.Conv2D(1, (1, 1),
                             name='Fuse',
                             padding='SAME',
                             use_bias=False,
                             activation=None,
                             kernel_initializer=fuseInitWeight,
                             kernel_regularizer=l1(weightDecay))(Fuse)

        # output1 = layers.Activation('sigmoid', name='output1')(Side1)
        # output2 = layers.Activation('sigmoid', name='output2')(Side2)
        # output3 = layers.Activation('sigmoid', name='output3')(Side3)
        # output4 = layers.Activation('sigmoid', name='output4')(Side4)
        # output5 = layers.Activation('sigmoid', name='output5')(Side5)
        output6 = layers.Activation('sigmoid', name='output6')(Fuse)

        outputs = [output6
                   ]  # [output1, output2, output3, output4, output5, output6]
        self._pModel = Model(inputs=Inputs, outputs=outputs)
        pOptimizer = optimizers.adam(lr=0.0001)
        pOptimizer = optimizers.SGD(lr=0.000001, decay=0., momentum=0.9)
        pOptimizer = tf.optimizers.SGD(lr=0.5, decay=0., momentum=0.9)
        # pOptimizer = monitorSGD(lr=0.000001, decay=0., momentum=0.9)
        # grads = tf.gradients(classBalancedSigmoidCrossEntropy, self._pModel.trainable_weights)
        # pSGD = optimizers.SGD()

        self._pModel.compile(
            loss={
                # 'output1': classBalancedSigmoidCrossEntropy,
                # 'output2': classBalancedSigmoidCrossEntropy,
                # 'output3': classBalancedSigmoidCrossEntropy,
                # 'output4': classBalancedSigmoidCrossEntropy,
                # 'output5': classBalancedSigmoidCrossEntropy,
                'output6': classBalancedSigmoidCrossEntropy
            },
            optimizer=pOptimizer)

        # self._pModel.summary()

    def startTrain(self, listDS, iMaxLen, iBatchSize):
        '''
        itrTrain = tf.compat.v1.data.make_one_shot_iterator(listDS[0])
        itrValid = tf.compat.v1.data.make_one_shot_iterator(listDS[1])

        iStepsPerEpochTrain = int(iMaxLen[0] / iBatchSize[0])
        iStepsPerEpochValid = int(iMaxLen[1] / iBatchSize[1])

        pBack = myCallback(self._strLogPath)

        self._pModel.fit(itrTrain, validation_data=itrValid, epochs=self._iEpochs,
                         callbacks=[self._pSaveModel, self._pTensorboard, pBack], steps_per_epoch=iStepsPerEpochTrain,
                         validation_steps=iStepsPerEpochValid)
        '''
        itrTrain = tf.compat.v1.data.make_one_shot_iterator(listDS[0])
        itrValid = tf.compat.v1.data.make_one_shot_iterator(listDS[1])
        iStepsPerEpochTrain = int(iMaxLen[0] / iBatchSize[0])
        iStepsPerEpochValid = int(iMaxLen[1] / iBatchSize[1])

        # trainLoss = tf.keras.metrics.Mean(name='train_loss')
        dictLossGroup = self._pModel.loss
        # t = self._pModel.layers[23].losses
        # t = self._pModel.weights[0].name
        # p = self._pModel.loss
        iTick = 0

        # epoch
        for epoch in range(self._iEpochs):
            # save model
            if iTick > self._iPeriod:
                strModelFileName = self._strModelFileName.format(epoch=epoch +
                                                                 1)
                filepath = self._strSavePath + strModelFileName
                print(self._strFormat %
                      ('Epoch: %s/%s, SaveModel: %s' %
                       (str(epoch), str(self._iEpochs), strModelFileName)))
                self._pModel.save_weights(filepath, overwrite=True)
                iTick = 0
            iTick += 1
            # stepsPerEpoch
            for stepsPerEpoch in range(iStepsPerEpochTrain):
                with tf.GradientTape() as tape:
                    itr = itrTrain.next()
                    # output define as [out1, out2, ....., out6]
                    listPredict = [self._pModel(itr[0])]
                    t = self._pModel.weights
                    listLabel = [itr[1]]

                    listLoss = []
                    fAllLoss = 0.
                    template = 'Per: {}/{}, TrainLoss: {}  --  '
                    i = 0

                    # multiple output, calculate loss
                    for key in dictLossGroup:
                        # loss function
                        pLoss = dictLossGroup[key]
                        # add regularize
                        regularization_loss = tf.math.add_n(
                            self._pModel.losses)
                        # pLoss += tf.add_n
                        # loss value
                        outputLoss = pLoss(
                            listLabel[i], listPredict[i]) + regularization_loss
                        listLoss.append(outputLoss)
                        # sum of loss
                        fAllLoss += outputLoss
                        # print format
                        template += 'train_loss_%s: {}  --  ' % key
                        i += 1

                    # calculate gradient
                    gradient = tape.gradient(fAllLoss,
                                             self._pModel.trainable_weights)

                    # trainLoss(fAllLoss)
                    template += '\n'
                    print(
                        template.format(stepsPerEpoch + 1, iStepsPerEpochTrain,
                                        fAllLoss, listLoss[0]))
                    # backprop
                    self._pModel.optimizer.apply_gradients(
                        zip(gradient, self._pModel.trainable_weights))

            # 每执行完一个train epoch 进行validcross 因此valid计算不能与train同步进行要在train epoch结束后进行
            fValidAllLoss = 0.
            listValidLoss = list(0 for n in range(len(dictLossGroup)))
            for stepsPerEpochValid in range(iStepsPerEpochValid):
                itr2 = itrValid.next()
                listPreValid = [self._pModel(itr2[0])]
                listValidLabel = [itr2[1]]
                i = 0
                for key in dictLossGroup:
                    # loss function
                    pLoss = dictLossGroup[key]
                    # loss value
                    outputValidLoss = pLoss(listValidLabel[i], listPreValid[i])
                    listValidLoss[i] += outputValidLoss
                    # sum of loss
                    fValidAllLoss += outputValidLoss
                    # print format
                    # template += '  --train_loss_%s: {}--  ' % key
                    i += 1

            # mean of val_loss
            fValidAllLoss /= iStepsPerEpochValid
            validTemplate = 'Epoch {}, val_loss: {}  --  '.format(
                epoch + 1, fValidAllLoss)
            for k in range(len(listValidLoss)):
                listValidLoss[k] /= iStepsPerEpochValid
                validTemplate += 'val_loss_{}: {}  --  '.format(
                    k + 1, listValidLoss[k])

            print(
                '\n-----------------------------------------------------------------------\n'
            )
            print(validTemplate)
            print(
                '\n-----------------------------------------------------------------------\n'
            )

            # per epoch output
            with self.__pTrainFW.as_default():
                i = 0
                tf.summary.scalar('loss: ', fAllLoss, step=epoch)
                # tf.summary.scalar('val_loss: ', fValidAllLoss, step=epoch)

                for key in dictLossGroup:
                    tf.summary.scalar('loss_' + key, listLoss[i], step=epoch)
                    # tf.summary.scalar('val_loss_' + key, listValidLoss[i], step=epoch)
                    i += 1

            with self.__pMetricsFW.as_default():
                # save gradient each layer
                pLayerWeight = self._pModel.trainable_weights
                for i in range(len(pLayerWeight)):
                    strName = pLayerWeight[i].name + '/Grad'
                    tf.summary.histogram(strName, gradient[i], step=epoch)
                    # mean grad
                    meanGrad = tf.reduce_mean(gradient[i])
                    tf.summary.scalar(strName + '/Mean', meanGrad, step=epoch)
                    # model grad
                    tensorNorm = tf.norm(gradient[i])
                    tf.summary.scalar(strName + '/Norm',
                                      tensorNorm,
                                      step=epoch)

            with self.__pValidFW.as_default():
                i = 0
                tf.summary.scalar('loss: ', fValidAllLoss, step=epoch)

                for key in dictLossGroup:
                    tf.summary.scalar('loss_' + key,
                                      listValidLoss[i],
                                      step=epoch)
                    i += 1

    def loadWeights(self, strPath):
        # last = tf.train.latest_checkpoint(strPath)
        # checkPoint = tf.train.load_checkpoint(strPath)
        self._pModel.load_weights(strPath)
        # w = self._pModel.weights
        # visual model
        self.__bLoadModel = True

    def generateVisualModel(self):
        outputs = []
        for myLayer in self._pModel.layers:
            self.__listLayerName.append(myLayer.name)
            outputs.append(myLayer.output)

        # print(self.__pModel.layers[0])
        # self.__pVisualModel = Model(self.__pModel.inputs, outputs=outputs)
        self.__pVisualModel = Model(self._pModel.inputs,
                                    outputs=self._pModel.outputs)
        return self.__pVisualModel

    def predict(self, IMG):
        # pImage = open(IMG, 'rb').read()
        # tensorIMG = tf.image.decode_jpeg(pImage)
        pIMG = image.array_to_img(IMG)  # .resize((256, 144))
        tensorIMG = image.img_to_array(pIMG)
        x = np.array(tensorIMG / 255.0)
        # show image
        iColumn = 4
        # generate window
        plt.figure(num='Input')
        # plt.subplot(1, 1, 1)
        plt.imshow(x)

        # imagetest = x

        x = np.expand_dims(x, axis=0)
        # pyplot.imshow(x)
        time1 = datetime.datetime.now()
        outputs = self.__pVisualModel.predict(x)
        time2 = datetime.datetime.now()
        print(time2 - time1)
        i = 100
        listOutput = []
        for i in range(len(outputs)):
            outputShape = outputs[i].shape
            singleOut = outputs[i].reshape(outputShape[0], outputShape[1],
                                           outputShape[2])
            # singleOut *= 255
            listOutput.append(singleOut)
        singleOut = listOutput[-1]
        singleOut[singleOut > 0.5] = 1
        listOutput[-1] = singleOut
        return listOutput
        '''
        for output in outputs:
            # plt.figure(num='%s' % str(i))
            outputShape = output.shape
            singleOut = output.reshape(outputShape[1], outputShape[2], outputShape[3])
            singleOut *= 255
            if outputShape[3] == 1:
                # test = x - output
                # test = np.abs(test)
                # return mysum

                # plt.subplot(1, 1, 1)
                # plt.imshow(singleOut, camp='gray')
                # cv2.imwrite('D:\wyc\Projects\TrainDataSet\HED\Result/%s.jpg' % str(i), singleOut)
                return singleOut
                # i += 1
                # plt.show()
        '''

    def getModelConfig(self):
        return self._iBatchSize
Evaluate and check
"""
if evaluate:
    progress = tqdm.tqdm(test_loader, total=len(test_loader))
    inp = Input(shape=(2048, ), name="dense")
    dense_layer = Model(inp, motion_model_restored.layers[-1].layers[-1](inp))

    video_level_preds_np = np.zeros(
        (len(progress), num_actions))  # each video per 101 class (prediction)
    video_level_labels_np = np.zeros((len(progress), 1))

    for index, (video_frames, video_label
                ) in enumerate(progress):  # i don't need frame level labels
        feature_field, frame_preds = motion_model_with_2_outputs.predict_on_batch(
            video_frames)
        assert np.allclose(frame_preds, dense_layer.predict(feature_field))

        video_level_preds_np[index, :] = np.mean(frame_preds, axis=0)
        video_level_labels_np[index, 0] = video_label

    video_level_loss, video_level_accuracy_1, video_level_accuracy_5 = keras.backend.get_session(
    ).run(
        [val_loss_op, acc_top_1_op, acc_top_5_op],
        feed_dict={
            video_level_labels_k: video_level_labels_np,
            video_level_preds_k: video_level_preds_np
        })

    print("Motion Model validation", "prec@1", video_level_accuracy_1,
          "prec@5", video_level_accuracy_5, "loss", video_level_loss)
"""
Esempio n. 25
0
    batch_size, num_frames, runner_in_seq_len = tuple(inputTensors[0].dims)
    _, _, runner_out_seq_len = tuple(outputTensors[0].dims)
    batches.append(batch_size)

out_pos = graph.get_root_subgraph().get_attr('output_fix2float')
in_pos = graph.get_root_subgraph().get_attr('input_float2fix')

print("Hardware ready")
hybrid_begin = datetime.datetime.now()

# print(model.get_weights())
print("Embedding start")
permute_layer_model = Model(inputs=model.input,
                            outputs=model.get_layer("embedding").output)
ebd_begin = datetime.datetime.now()
pd = permute_layer_model.predict(X_test, batch_size=8)
ebd_end = datetime.datetime.now()
num_records = pd.shape[0]
print("Embedding over")

cv1b = datetime.datetime.now()
quantized_lstm_input = quanti_convert_float_to_int16(
    pd.reshape(num_records * 16000), in_pos).reshape((num_records, 16000))
cv1e = datetime.datetime.now()

lstm_output = np.ones((quantized_lstm_input.shape[0], 100), dtype=np.int16)
print("Start LSTMlayer")
begin = datetime.datetime.now()

frame_num = 500
frame_size = 16000 * 2
Esempio n. 26
0
    batch_size = tuple(inputTensors[0].dims)[0]
    batches.append(batch_size)

out_pos = graph.get_root_subgraph().get_attr('output_fix2float')
in_pos = graph.get_root_subgraph().get_attr('input_float2fix')

print("Hardware ready")
hybrid_begin = datetime.datetime.now()

# print(model.get_weights())
print("Embedding start")
permute_layer_model = Model(
    inputs=model.input, outputs=model.get_layer("embedding").output)
# print(permute_layer_model.get_layer("embedding").get_weights())
ebd_begin = datetime.datetime.now()
lstm_input = permute_layer_model.predict(X_test, batch_size=8)
ebd_end = datetime.datetime.now()
num_records = lstm_input.shape[0]
print("Embedding over")

cv1b = datetime.datetime.now()
quantized_lstm_input = quanti_convert_float_to_int16(lstm_input.reshape(
    num_records * 16000), in_pos).reshape((num_records, 16000))
cv1e = datetime.datetime.now()
inputs = []
lstm_output = np.zeros((quantized_lstm_input.shape[0], 100), dtype=np.int16)
print("Start LSTMlayer")
begin = datetime.datetime.now()

# use the multi-batch
inputTensors = runners[0].get_input_tensors()
Esempio n. 27
0
class jyHEDModelV1(jyModelBase):
    def __init__(self):
        super(jyHEDModelV1, self).__init__()
        self.__listLayerName = []
        self.__pVisualModel = None

    def structureModel(self):
        Inputs = layers.Input(shape=self._inputShape, batch_size=self._iBatchSize)
        Con1 = layers.Conv2D(64, (3, 3), name='Con1', activation='relu', padding='SAME', input_shape=self._inputShape, strides=1)(Inputs)
        Con2 = layers.Conv2D(64, (3, 3), name='Con2', activation='relu', padding='SAME', strides=1)(Con1)
        Side1 = sideBranch(Con2, 1)
        MaxPooling1 = layers.MaxPooling2D((2, 2), name='MaxPooling1', strides=2, padding='SAME')(Con2)
        # outputs1
        Con3 = layers.Conv2D(128, (3, 3), name='Con3', activation='relu', padding='SAME', strides=1)(MaxPooling1)
        Con4 = layers.Conv2D(128, (3, 3), name='Con4', activation='relu', padding='SAME', strides=1)(Con3)
        Side2 = sideBranch(Con4, 2)
        MaxPooling2 = layers.MaxPooling2D((2, 2), name='MaxPooling2', strides=2, padding='SAME')(Con4)
        # outputs2
        Con5 = layers.Conv2D(256, (3, 3), name='Con5', activation='relu', padding='SAME', strides=1)(MaxPooling2)
        Con6 = layers.Conv2D(256, (3, 3), name='Con6', activation='relu', padding='SAME', strides=1)(Con5)
        Con7 = layers.Conv2D(256, (3, 3), name='Con7', activation='relu', padding='SAME', strides=1)(Con6)
        Side3 = sideBranch(Con7, 4)
        MaxPooling3 = layers.MaxPooling2D((2, 2), name='MaxPooling3', strides=2, padding='SAME')(Con7)
        # outputs3
        Con8 = layers.Conv2D(512, (3, 3), name='Con8', activation='relu', padding='SAME', strides=1)(MaxPooling3)
        Con9 = layers.Conv2D(512, (3, 3), name='Con9', activation='relu', padding='SAME', strides=1)(Con8)
        Con10 = layers.Conv2D(512, (3, 3), name='Con10', activation='relu', padding='SAME', strides=1)(Con9)
        Side4 = sideBranch(Con10, 8)
        MaxPooling4 = layers.MaxPooling2D((2, 2), name='MaxPooling4', strides=2, padding='SAME')(Con10)
        # outputs4
        Con11 = layers.Conv2D(512, (3, 3), name='Con11', activation='relu', padding='SAME', strides=1)(MaxPooling4)
        Con12 = layers.Conv2D(512, (3, 3), name='Con12', activation='relu', padding='SAME', strides=1)(Con11)
        Con13 = layers.Conv2D(512, (3, 3), name='Con13', activation='relu', padding='SAME', strides=1)(Con12)
        Side5 = sideBranch(Con13, 16)
        Fuse = layers.Concatenate(axis=-1)([Side1, Side2, Side3, Side4, Side5])

        # learn fusion weight
        Fuse = layers.Conv2D(1, (1, 1), name='Fuse', padding='SAME', use_bias=False, activation=None)(Fuse)

        output1 = layers.Activation('sigmoid', name='output1')(Side1)
        output2 = layers.Activation('sigmoid', name='output2')(Side2)
        output3 = layers.Activation('sigmoid', name='output3')(Side3)
        output4 = layers.Activation('sigmoid', name='output4')(Side4)
        output5 = layers.Activation('sigmoid', name='output5')(Side5)
        output6 = layers.Activation('sigmoid', name='output6')(Fuse)

        outputs = [output1, output2, output3, output4, output5, output6]
        self._pModel = Model(inputs=Inputs, outputs=outputs)
        pAdam = optimizers.adam(lr=0.0001)
        self._pModel.compile(loss={
                                   'output6': classBalancedSigmoidCrossEntropy
                                   }, optimizer=pAdam)

        # self._pModel.summary()

    def startTrain(self, listDS, iMaxLen, iBatchSize):
        itrTrain = tf.compat.v1.data.make_one_shot_iterator(listDS[0])
        itrValid = tf.compat.v1.data.make_one_shot_iterator(listDS[1])

        iStepsPerEpochTrain = int(iMaxLen[0] / iBatchSize[0])
        iStepsPerEpochValid = int(iMaxLen[1] / iBatchSize[1])

        self._pModel.fit(itrTrain, validation_data=itrValid, epochs=self._iEpochs,
                         callbacks=[self._pSaveModel, self._pTensorboard], steps_per_epoch=iStepsPerEpochTrain,
                         validation_steps=iStepsPerEpochValid)

    def loadWeights(self, strPath):
        # last = tf.train.latest_checkpoint(strPath)
        # checkPoint = tf.train.load_checkpoint(strPath)
        self._pModel.load_weights(strPath)
        # visual model
        outputs = []

        for myLayer in self._pModel.layers:
            self.__listLayerName.append(myLayer.name)
            outputs.append(myLayer.output)

        # print(self.__pModel.layers[0])
        # self.__pVisualModel = Model(self.__pModel.inputs, outputs=outputs)
        self.__pVisualModel = Model(self._pModel.inputs, outputs=self._pModel.outputs)
        return self.__pVisualModel

    def predict(self, IMG):
        # pImage = open(IMG, 'rb').read()
        # tensorIMG = tf.image.decode_jpeg(pImage)
        pIMG = image.array_to_img(IMG)# .resize((256, 144))
        tensorIMG = image.img_to_array(pIMG)
        x = np.array(tensorIMG / 255.0)
        # show image
        iColumn = 4
        # generate window
        plt.figure(num='Input')
        # plt.subplot(1, 1, 1)
        plt.imshow(x)

        # imagetest = x

        x = np.expand_dims(x, axis=0)
        # pyplot.imshow(x)
        time1 = datetime.datetime.now()
        outputs = self.__pVisualModel.predict(x)
        time2 = datetime.datetime.now()
        print(time2 - time1)
        i = 100
        listOutput = []
        for i in range(len(outputs)):
            outputShape = outputs[i].shape
            singleOut = outputs[i].reshape(outputShape[1], outputShape[2], outputShape[3])
        # singleOut *= 255
            listOutput.append(singleOut)
        singleOut = listOutput[-1]
        singleOut[singleOut > 0.5] = 1
        listOutput[-1] = singleOut
        return listOutput
        '''
        for output in outputs:
            # plt.figure(num='%s' % str(i))
            outputShape = output.shape
            singleOut = output.reshape(outputShape[1], outputShape[2], outputShape[3])
            singleOut *= 255
            if outputShape[3] == 1:
                # test = x - output
                # test = np.abs(test)
                # return mysum

                # plt.subplot(1, 1, 1)
                # plt.imshow(singleOut, camp='gray')
                # cv2.imwrite('D:\wyc\Projects\TrainDataSet\HED\Result/%s.jpg' % str(i), singleOut)
                return singleOut
                # i += 1
                # plt.show()
        '''
    def getModelConfig(self):
        return self._iBatchSize
Esempio n. 28
0
class GenericModel:
    @staticmethod
    def load_from(path):
        model = GenericModel()
        model.model = load_model(path)
        return model

    def __init__(self):
        self.model = None
        self.registered_callbacks = []
        self.id = 'generic_model'
        self.time = round(time())
        self.desc = None
        """config = ConfigProto()
        config.gpu_options.per_process_gpu_memory_fraction = 0.40
        config.gpu_options.allow_growth = True
        session = InteractiveSession(config=config)"""

    def build_model(self):
        img_input = Input(self.get_input_shape())
        last_layer = self.model_structure(img_input)
        self.model = Model(img_input, last_layer)
        self.model.summary()

    def compile(self,
                loss_function,
                metric_functions=None,
                optimizer=Adam(1e-3, epsilon=1e-6)):
        self.require_model_loaded()
        return self.model.compile(loss=loss_function,
                                  optimizer=optimizer,
                                  metrics=metric_functions)

    def model_structure(self, input_img):
        raise NotImplementedError

    def get_input_shape(self):
        raise NotImplementedError

    def register_std_callbacks(self,
                               tensorboard_logs_folder=None,
                               checkpoint_path=None):
        self.require_model_loaded()
        run_id = str(time())
        if self.desc is not None:
            run_id += "_" + self.desc
        folder_id = os.path.join(self.id, run_id)
        if tensorboard_logs_folder is not None:
            self.registered_callbacks.append(
                TensorBoard(log_dir=os.path.join(tensorboard_logs_folder,
                                                 folder_id),
                            histogram_freq=0,
                            write_graph=True,
                            write_images=True))

        if checkpoint_path is not None:
            store_path = os.path.join(checkpoint_path, folder_id)
            if not os.path.exists(store_path):
                os.makedirs(store_path)
            store_path = os.path.join(
                store_path, 'e{epoch:02d}-l{loss:.4f}-v{val_loss:.4f}.ckpt')
            print("Storing to %s" % store_path)
            self.registered_callbacks.append(
                ModelCheckpoint(store_path,
                                monitor='val_loss',
                                verbose=1,
                                period=1,
                                save_best_only=False,
                                mode='min'))

    def train_with_generator(self,
                             training_data_generator,
                             epochs,
                             steps_per_epoch,
                             validation_data=None):
        self.model.fit(training_data_generator,
                       use_multiprocessing=True,
                       workers=4,
                       steps_per_epoch=steps_per_epoch,
                       callbacks=self.registered_callbacks,
                       epochs=epochs,
                       verbose=1,
                       **({} if validation_data is None else {
                           "validation_data": validation_data
                       }))

    def require_model_loaded(self):
        if self.model is None:
            raise ValueError("Model is not build yet")

    def load_weights(self, path):
        self.require_model_loaded()
        return self.model.load_weights(path)

    def predict(self, batch):
        self.require_model_loaded()
        return self.model.predict(batch)
Esempio n. 29
0
class BaseKerasModel(BaseModel):
    model = None
    tensorboard = None
    train_names = ['train_loss', 'train_mse', 'train_mae']
    val_names = ['val_loss', 'val_mse', 'val_mae']
    counter = 0
    inputs = None
    hidden_layer = None
    outputs = None

    def __init__(self,
                 use_default_dense=True,
                 activation='relu',
                 kernel_regularizer=tf.keras.regularizers.l1(0.001)):
        super().__init__()
        if use_default_dense:
            self.activation = activation
            self.kernel_regularizer = kernel_regularizer

    def create_input_layer(self, input_placeholder: BaseInputFormatter):
        """Creates keras model"""
        self.inputs = tf.keras.layers.InputLayer(
            input_shape=input_placeholder.get_input_state_dimension())
        return self.inputs

    def create_hidden_layers(self, input_layer=None):
        if input_layer is None:
            input_layer = self.inputs
        hidden_layer = tf.keras.layers.Dropout(0.3)(input_layer)
        hidden_layer = tf.keras.layers.Dense(
            128,
            kernel_regularizer=self.kernel_regularizer,
            activation=self.activation)(hidden_layer)
        hidden_layer = tf.keras.layers.Dropout(0.4)(hidden_layer)
        hidden_layer = tf.keras.layers.Dense(
            64,
            kernel_regularizer=self.kernel_regularizer,
            activation=self.activation)(hidden_layer)
        hidden_layer = tf.keras.layers.Dropout(0.3)(hidden_layer)
        hidden_layer = tf.keras.layers.Dense(
            32,
            kernel_regularizer=self.kernel_regularizer,
            activation=self.activation)(hidden_layer)
        hidden_layer = tf.keras.layers.Dropout(0.1)(hidden_layer)
        self.hidden_layer = hidden_layer
        return self.hidden_layer

    def create_output_layer(self,
                            output_formatter: BaseOutputFormatter,
                            hidden_layer=None):
        # sigmoid/tanh all you want on self.model
        if hidden_layer is None:
            hidden_layer = self.hidden_layer
        self.outputs = tf.keras.layers.Dense(
            output_formatter.get_model_output_dimension()[0],
            activation='tanh')(hidden_layer)
        self.model = Model(inputs=self.inputs, outputs=self.outputs)
        return self.outputs

    def write_log(self, callback, names, logs, batch_no, eval=False):
        for name, value in zip(names, logs):
            summary = tf.Summary()
            summary_value = summary.value.add()
            summary_value.simple_value = value
            tag_name = name
            if eval:
                tag_name = 'eval_' + tag_name
            summary_value.tag = tag_name
            callback.writer.add_summary(summary, batch_no)
            callback.writer.flush()

    def finalize_model(self, logname=str(int(random() * 1000))):

        loss, loss_weights = self.create_loss()
        self.model.compile(tf.keras.optimizers.Nadam(lr=0.001),
                           loss=loss,
                           loss_weights=loss_weights,
                           metrics=[
                               tf.keras.metrics.mean_absolute_error,
                               tf.keras.metrics.binary_accuracy
                           ])
        log_name = './logs/' + logname
        self.logger.info("log_name: " + log_name)
        self.tensorboard = tf.keras.callbacks.TensorBoard(
            log_dir=log_name,
            histogram_freq=1,
            write_images=False,
            batch_size=1000,
        )
        self.tensorboard.set_model(self.model)
        self.logger.info("Model has been finalized")

    def fit(self, x, y, batch_size=1):
        if self.counter % 200 == 0:
            logs = self.model.evaluate(x, y, batch_size=batch_size, verbose=1)
            self.write_log(self.tensorboard,
                           self.model.metrics_names,
                           logs,
                           self.counter,
                           eval=True)
            print('step:', self.counter)
        else:
            logs = self.model.train_on_batch(x, y)
            self.write_log(self.tensorboard, self.model.metrics_names, logs,
                           self.counter)
        self.counter += 1

    def predict(self, arr):
        return self.model.predict(arr)

    def save(self, file_path):
        self.model.save_weights(filepath=file_path, overwrite=True)

    def load(self, file_path):
        path = os.path.abspath(file_path)
        self.model.load_weights(filepath=os.path.abspath(file_path))

    def create_loss(self):
        return 'mean_absolute_error', None
Esempio n. 30
0
np.random.seed(7)
# load the dataset top n words only
pre_end = datetime.datetime.now()

a = np.array(y_test)
a = a.reshape([a.shape[0], 1])
print("running on tensorflow cpu")

t1 = time.time()

embedding_vecor_length = 32
embd_model = Model(inputs=model.input,
                   outputs=model.get_layer("embedding").output)

ebd_begin = datetime.datetime.now()
embd_output = embd_model.predict(X_test, batch_size=1)
ebd_end = datetime.datetime.now()

original_lstm = Model(inputs=model.input,
                      outputs=model.get_layer('lstm').output)
cpu_lstm_begin = datetime.datetime.now()
original_lstm_output = original_lstm.predict(X_test, batch_size=1024)
cpu_lstm_end = datetime.datetime.now()

lstm_model2 = Sequential()
lstm_model2.add(Dense(1, activation='sigmoid'))
lstm_model2.build((1, 100))
layer_dict_fix = dict([(layer.name, layer) for layer in model.layers])
lstm_model2.layers[0].set_weights(layer_dict_fix['dense'].get_weights())

dense_begin = datetime.datetime.now()