This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def feature_reconstruction_loss(y_true, y_pred): | |
| """This function will receive a tensor that | |
| already calculated the square error. | |
| So, just calculate the average here. | |
| """ | |
| return lambda_f * K.mean(y_pred) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def connect_vgg16(self): | |
| # We need connect FastStyleNet and vgg16 | |
| # to train FastStyleNet. | |
| fsn = self.create_model() | |
| fsn.name = "FastStyleNet" | |
| # Frozen all layers of vgg16. | |
| for l in vgg16.layers: | |
| l.trainable = False |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def generate_arrays_from_file(): | |
| while True: | |
| for path in imagepaths: | |
| contents_img = load_image(path, image_size) | |
| # We should not do to get feature from vgg16 as follows | |
| # becouse calc with cpu here. | |
| c_feature = vgg16(contents_img) | |
| yield (contents_img, [_1, _2, _3, _4, c_feature, _6]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """Dummy arrays | |
| When we use fit(X, y) function, | |
| we must set same shape arrays between X and y. | |
| However, we want to apply array of different shape to the objective function. | |
| So, we should prepare for Dummy arrays. | |
| """ | |
| _1 = np.empty((1, 256, 256, 64)) | |
| _2 = np.empty((1, 128, 128, 128)) | |
| _3 = np.empty((1, 64, 64, 256)) | |
| _4 = np.empty((1, 32, 32, 512)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def style_reconstruction_loss(gram_s): | |
| """we should calculate gram matrix of style image just once. | |
| Therefore, I implemented this function like this. | |
| """ | |
| def loss_function(y_true, y_pred): | |
| gram_s_hat = gram_matrix(y_pred) | |
| return lambda_s * K.mean(K.square(gram_s_hat - gram_s)) | |
| return loss_function | |
| model.compile(optimizer=adam, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extern crate gnuplot; | |
| extern crate nalgebra; | |
| use std::f64; | |
| use std::vec::Vec; | |
| use gnuplot::{Figure, Color}; | |
| fn main() { | |
| // 平均 0, 分散 1 としてインスタンス化する. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def binarylab(labels): | |
| x = np.zeros([224,224,21]) | |
| for i in range(224): | |
| for j in range(224): | |
| x[i,j,labels[i][j]]=1 | |
| return x | |
| def generate_arrays_from_file(path): | |
| while 1: | |
| with open(path,"r") as f: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def train(path): | |
| model = create_model() | |
| model.compile(loss="categorical_crossentropy", | |
| optimizer='adadelta', | |
| metrics=["accuracy"]) | |
| model.fit_generator(generate_arrays_from_file(path), | |
| samples_per_epoch=1450, nb_epoch=100) | |
| model.save_weights('weights.hdf5') | |
| return |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def create_model(): | |
| FCN_CLASSES = 21 | |
| #(samples, channels, rows, cols) | |
| input_img = Input(shape=(3, 224, 224)) | |
| #(3*224*224) | |
| x = Convolution2D(64, 3, 3, activation='relu',border_mode='same')(input_img) | |
| x = Convolution2D(64, 3, 3, activation='relu',border_mode='same')(x) | |
| x = MaxPooling2D((2, 2), strides=(2, 2))(x) |