-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmodel.py
81 lines (69 loc) · 2.88 KB
/
model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# coding:utf-8
import os, sys
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from PIL import Image
import h5py
import keras.backend as K
from keras.models import Model, Sequential
from keras.layers import Input, Dense, Flatten, Dropout, Activation, Reshape
from keras.layers.advanced_activations import LeakyReLU
from keras.layers.pooling import MaxPooling2D, GlobalAveragePooling2D
from keras.layers.normalization import BatchNormalization
from keras.layers.convolutional import Conv2D, Conv2DTranspose, UpSampling2D
from keras.initializers import RandomNormal
init = RandomNormal(mean = 0., stddev = 0.02)
# ConvTranspose (often called Deconv) ver.
def GeneratorDeconv(image_size = 64):
L = int(image_size)
inputs = Input(shape = (100, ))
x = Dense(512*int(L/16)**2)(inputs) #shape(512*(L/16)**2,)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Reshape((int(L/16), int(L/16), 512))(x) # shape(L/16, L/16, 512)
x = Conv2DTranspose(256, (4, 4), strides = (2, 2),
kernel_initializer = init,
padding = 'same')(x) # shape(L/8, L/8, 256)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2DTranspose(128, (4, 4), strides = (2, 2),
kernel_initializer = init,
padding = 'same')(x) # shape(L/4, L/4, 128)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2DTranspose(64, (4, 4), strides = (2, 2),
kernel_initializer = init,
padding = 'same')(x) # shape(L/2, L/2, 64)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2DTranspose(3, (4, 4), strides= (2, 2),
kernel_initializer = init,
padding = 'same')(x) # shape(L, L, 3)
images = Activation('tanh')(x)
model = Model(inputs = inputs, outputs = images)
model.summary()
return model
def Discriminator(image_size = 64):
L = int(image_size)
images = Input(shape = (L, L, 3))
x = Conv2D(64, (4, 4), strides = (2, 2),
kernel_initializer = init, padding = 'same')(images) # shape(L/2, L/2, 32)
x = LeakyReLU(0.2)(x)
x = Conv2D(128, (4, 4), strides = (2, 2),
kernel_initializer = init, padding = 'same')(x) # shape(L/4, L/4, 64)
x = BatchNormalization()(x)
x = LeakyReLU(0.2)(x)
x = Conv2D(256, (4, 4), strides = (2, 2),
kernel_initializer = init, padding = 'same')(x) # shape(L/8, L/8, 128)
x = BatchNormalization()(x)
x = LeakyReLU(0.2)(x)
x = Conv2D(512, (4, 4), strides = (2, 2),
kernel_initializer = init, padding = 'same')(x) # shape(L/16, L/16, 256)
x = BatchNormalization()(x)
x = LeakyReLU(0.2)(x)
x = Flatten()(x)
outputs = Dense(1)(x)
model = Model(inputs = images, outputs = outputs)
model.summary()
return model