-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModel.py
78 lines (71 loc) · 3.15 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
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 7 15:53:43 2022
@author: Admin
"""
import tensorflow as tf
from tensorflow.keras.layers import Input,Conv2DTranspose,LeakyReLU,Flatten,Dense,Dropout,BatchNormalization,Reshape,Activation,Conv2D
from tensorflow.keras.models import Model
class Model:
def __init__(self,latent_dim,image_shape,**kwargs):
self.latent_dim=latent_dim
self.image_shape=image_shape
def build_generator(self,latent_dim):
"""
latent dimension for image creation
"""
n_nodes = 8* 8*16
noise = Input(shape=(latent_dim,), name="generator_noise_input")
x = Dense(n_nodes, use_bias=False)(noise)
x = BatchNormalization()(x)
x = LeakyReLU(alpha=0.2)(x)
x = Reshape((8, 8, 16 ))(x)
x=Conv2DTranspose(16, kernel_size=3,strides=(2,2), padding='same')(x)
x = BatchNormalization()(x)
x = LeakyReLU(alpha=0.2)(x)
x=Conv2DTranspose(32,kernel_size=3,strides=(2,2), padding='same')(x)
x = BatchNormalization()(x)
x = LeakyReLU(alpha=0.2)(x)
x=Conv2DTranspose(32,kernel_size=3,strides=(2,2), padding='same')(x)
x = BatchNormalization()(x)
x = LeakyReLU(alpha=0.2)(x)
x=Conv2DTranspose(32,kernel_size=3,strides=(2,2), padding='same')(x)
x = BatchNormalization()(x)
x = LeakyReLU(alpha=0.2)(x)
x=Conv2DTranspose(32,kernel_size=3,strides=(2,2), padding='same')(x)
x = BatchNormalization()(x)
x = LeakyReLU(alpha=0.2)(x)
x=Conv2DTranspose(3, kernel_size=3,strides=(2,2), padding='same')(x)
x = BatchNormalization()(x)
x = LeakyReLU(alpha=0.2)(x)
fake_output = Activation("tanh")(x)
return Model(noise, fake_output, name="generator")
def build_discriminator(self,image_shape):
"""
image shape as tupe of (H,W,C)
"""
w_init = tf.keras.initializers.RandomNormal(mean=0.0, stddev=0.02)
image_input = Input(shape=image_shape)
x=Conv2D(16, kernel_size=5, strides=2,kernel_initializer=w_init,padding="same")(image_input)
x = LeakyReLU(alpha=0.2)(x)
x = Dropout(0.3)(x)
x=Conv2D(16, kernel_size=5, strides=2,kernel_initializer=w_init,padding="same")(x)
x = LeakyReLU(alpha=0.2)(x)
x = Dropout(0.3)(x)
x=Conv2D(16, kernel_size=5, strides=2,kernel_initializer=w_init,padding="same")(x)
x = LeakyReLU(alpha=0.2)(x)
x = Dropout(0.3)(x)
x=Conv2D(16, kernel_size=5, strides=2,kernel_initializer=w_init,padding="same")(x)
x = LeakyReLU(alpha=0.2)(x)
x = Dropout(0.3)(x)
x=Conv2D(16, kernel_size=5, strides=2,kernel_initializer=w_init,padding="same")(x)
x = LeakyReLU(alpha=0.2)(x)
x = Dropout(0.3)(x)
x=Conv2D(16, kernel_size=5, strides=2,kernel_initializer=w_init,padding="same")(x)
x = LeakyReLU(alpha=0.2)(x)
x = Dropout(0.3)(x)
x = Flatten()(x)
x = Dense(16)(x)
x = Dense(8)(x)
x = Dense(1)(x)
return Model(image_input, x, name="discriminator")