Skip to content

Instantly share code, notes, and snippets.

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)
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
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])
@k3nt0w
k3nt0w / fit.py
Last active February 11, 2017 10:13
"""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))
@k3nt0w
k3nt0w / loss.py
Last active February 11, 2017 09:54
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,
extern crate gnuplot;
extern crate nalgebra;
use std::f64;
use std::vec::Vec;
use gnuplot::{Figure, Color};
fn main() {
// 平均 0, 分散 1 としてインスタンス化する.
@k3nt0w
k3nt0w / data.py
Created October 31, 2016 18:43
generator
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:
@k3nt0w
k3nt0w / train.py
Created October 31, 2016 18:42
trainer
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
@k3nt0w
k3nt0w / train.py
Created October 31, 2016 17:08
create model for FCN
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)