Kaggle competition: Generative Dog Images
<SOS> metric: PostProcessorKernel Kaggle data source: generative-dog-images<compute_train_metric>
Conv2DTranspose, Conv2D, Flatten, Dropout, Embedding, ReLU
print(os.listdir(".. /input"))
global_score = f1_score(y_test, clf_final.predict(X_test), average='macro')
 vc.voting = 'soft'
global_score_soft = f1_score(y_test, vc.predict(X_test), average='macro')
 vc.voting = 'hard'
global_score_hard = f1_score(y_test, vc.predict(X_test), average='macro')
 print('Validation score of a single LGBM Classifier: {:.4f}'.format(global_score))
print('Validation score of a VotingClassifier on 3 LGBMs with soft voting strategy: {:.4f}'.format(global_score_soft))
print('Validation score of a VotingClassifier on 3 LGBMs with hard voting strategy: {:.4f}'.format(global_score_hard))<compute_test_metric>
tf.enable_eager_execution()
ranked_features = feature_importance(clf_final, X_train )<create_dataframe>
image_width = 64
image_height = 64
image_channels = 3
image_sample_size = 10000
image_output_dir = '.. /output_images/'
image_input_dir = '.. /input/all-dogs/all-dogs/'
image_ann_dir = ".. /input/annotation/Annotation/"
OUT_DIR = Path('.. /output_images/')
 MODEL_PATH = '.. /input/dog-face-generation-competition-kid-metric-input/classify_image_graph_def.pb'
TRAIN_DIR = Path('.. /input/all-dogs/all-dogs' )
y_subm = pd.DataFrame()
y_subm['Id'] = test_ids<predict_on_test>
dog_breed_dict = {}
for annotation in os.listdir(image_ann_dir):
annotations = annotation.split('-')
 dog_breed_dict[annotations[0]] = annotations[1]
y_subm['Target'] = clf_final.predict(test[X_train.columns])+ 1
vc.voting = 'soft'
y_subm_soft = y_subm.copy(deep=True)
 y_subm_soft['Target'] = vc.predict(test[X_train.columns])+ 1
vc.voting = 'hard'
y_subm_hard = y_subm.copy(deep=True)
 y_subm_hard['Target'] = vc.predict(test[X_train.columns])+ 1<save_to_csv>
def load_cropped_images(dog_breed_dict=dog_breed_dict, image_ann_dir=image_ann_dir, sample_size=25000,
image_width=image_width, image_height=image_height, image_channels=image_channels):
curIdx = 0
breeds = []
dog_images_np = np.zeros(( sample_size,image_width,image_height,image_channels))
for breed_folder in os.listdir(image_ann_dir):
for dog_ann in tqdm(os.listdir(image_ann_dir + breed_folder)) :
try:
img = read_image(os.path.join(image_input_dir, dog_ann + '.jpg'))
except FileNotFoundError:
continue
tree = ET.parse(os.path.join(image_ann_dir + breed_folder, dog_ann))
root = tree.getroot()
size = root.find('size')
 width = int(size.find('width' ).text)
 height = int(size.find('height' ).text)
 objects = root.findall('object')
 for o in objects:
bndbox = o.find('bndbox')
 xmin = int(bndbox.find('xmin' ).text)
 ymin = int(bndbox.find('ymin' ).text)
 xmax = int(bndbox.find('xmax' ).text)
 ymax = int(bndbox.find('ymax' ).text)
 xmin = max(0, xmin - 4)
 xmax = min(width, xmax + 4)
 ymin = max(0, ymin - 4)
 ymax = min(height, ymax + 4)
 w = np.min(( xmax - xmin, ymax - ymin))
w = min(w, width, height)
 if w > xmax - xmin:
xmin = min(max(0, xmin - int(( w -(xmax - xmin)) /2)) , width - w)
 xmax = xmin + w
if w > ymax - ymin:
ymin = min(max(0, ymin - int(( w -(ymax - ymin)) /2)) , height - w)
 ymax = ymin + w
img_cropped = img[ymin:ymin+w, xmin:xmin+w, :]
if xmax - xmin > image_width:
interpolation = cv2.INTER_AREA
else:
interpolation = cv2.INTER_CUBIC
img_cropped = cv2.resize(img_cropped,(image_width, image_height),
interpolation=interpolation)
 dog_images_np[curIdx,:,:,:] = np.asarray(img_cropped)
 dog_breed_name = dog_breed_dict[dog_ann.split('_')[0]]
breeds.append(dog_breed_name)
 curIdx += 1
return dog_images_np, breeds
now = datetime.now()
sub_file = 'submission_LGB_{:.4f}_{}.csv'.format(global_score, str(now.strftime('%Y-%m-%d-%H-%M')))
 sub_file_soft = 'submission_soft_LGB_{:.4f}_{}.csv'.format(global_score_soft, str(now.strftime('%Y-%m-%d-%H-%M')))
 sub_file_hard = 'submission_hard_LGB_{:.4f}_{}.csv'.format(global_score_hard, str(now.strftime('%Y-%m-%d-%H-%M')))
 sub_file_0forNonHeads = 'submission_0forNonHead_LGB_{:.4f}_{}.csv'.format(global_score, str(now.strftime('%Y-%m-%d-%H-%M')))
 y_subm.to_csv(sub_file, index=False)
 y_subm_soft.to_csv(sub_file_soft, index=False)
 y_subm_hard.to_csv(sub_file_hard, index=False )<set_options>
start_time = time.time()
dog_images_np, breeds = load_cropped_images(sample_size=22125)
 est_time = round(time.time() - start_time)
 print("Feature loading time: {}.".format(str(datetime.timedelta(seconds=est_time))))
%matplotlib inline
warnings.filterwarnings("ignore" )<feature_engineering>
dog_images_np =(dog_images_np - 127.5)/ 127.5
def do_features(df):
feats_div = [('children_fraction', 'r4t1', 'r4t3'), 
('working_man_fraction', 'r4h2', 'r4t3'), 
('all_man_fraction', 'r4h3', 'r4t3'), 
('human_density', 'tamviv', 'rooms'), 
('human_bed_density', 'tamviv', 'bedrooms'), 
('rent_per_person', 'v2a1', 'r4t3'), 
('rent_per_room', 'v2a1', 'rooms'), 
('mobile_density', 'qmobilephone', 'r4t3'), 
('tablet_density', 'v18q1', 'r4t3'), 
('mobile_adult_density', 'qmobilephone', 'r4t2'), 
('tablet_adult_density', 'v18q1', 'r4t2'),
]
feats_sub = [('people_not_living', 'tamhog', 'tamviv'), 
('people_weird_stat', 'tamhog', 'r4t3')]
for f_new, f1, f2 in feats_div:
df['fe_' + f_new] =(df[f1] / df[f2] ).astype(np.float32)
 for f_new, f1, f2 in feats_sub:
df['fe_' + f_new] =(df[f1] - df[f2] ).astype(np.float32)
 aggs_num = {'age': ['min', 'max', 'mean'],
'escolari': ['min', 'max', 'mean']
}
aggs_cat = {'dis': ['mean']}
for s_ in ['estadocivil', 'parentesco', 'instlevel']:
for f_ in [f_ for f_ in df.columns if f_.startswith(s_)]:
aggs_cat[f_] = ['mean', 'count']
for name_, df_ in [('18', df.query('age >= 18')) ]:
df_agg = df_.groupby('idhogar' ).agg({**aggs_num, **aggs_cat} ).astype(np.float32)
 df_agg.columns = pd.Index(['agg' + name_ + '_' + e[0] + "_" + e[1].upper() for e in df_agg.columns.tolist() ])
 df = df.join(df_agg, how='left', on='idhogar')
 del df_agg
df.drop([f_ for f_ in df.columns if f_.startswith('SQB')or f_ == 'agesq'], axis=1, inplace=True)
 df.drop(['Id'], axis=1, inplace=True)
 df.drop(['hhsize', 'female', 'area2'], axis=1, inplace=True)
 return df<categorify>
dog_features_tf = tf.cast(dog_images_np, 'float32' )
def convert_OHE2LE(df):
tmp_df = df.copy(deep=True)
 for s_ in ['pared', 'piso', 'techo', 'abastagua', 'sanitario', 'energcocinar', 'elimbasu',
'epared', 'etecho', 'eviv', 'estadocivil', 'parentesco',
'instlevel', 'lugar', 'tipovivi',
'manual_elec']:
if 'manual_' not in s_:
cols_s_ = [f_ for f_ in df.columns if f_.startswith(s_)]
elif 'elec' in s_:
cols_s_ = ['public', 'planpri', 'noelec', 'coopele']
sum_ohe = tmp_df[cols_s_].sum(axis=1 ).unique()
if 0 in sum_ohe:
print('The OHE in {} is incomplete.A new column will be added before label encoding' 
.format(s_))
col_dummy = s_+'_dummy'
tmp_df[col_dummy] =(tmp_df[cols_s_].sum(axis=1)== 0 ).astype(np.int8)
 cols_s_.append(col_dummy)
 sum_ohe = tmp_df[cols_s_].sum(axis=1 ).unique()
if 0 in sum_ohe:
print("The category completion did not work")
 tmp_cat = tmp_df[cols_s_].idxmax(axis=1)
 tmp_df[s_ + '_LE'] = LabelEncoder().fit_transform(tmp_cat ).astype(np.int16)
 if 'parentesco1' in cols_s_:
cols_s_.remove('parentesco1')
 tmp_df.drop(cols_s_, axis=1, inplace=True)
 return tmp_df<load_from_csv>
sample_size = 22125
batch_size = 128
weight_init_std = 0.02
weight_init_mean = 0.0
leaky_relu_slope = 0.2
downsize_factor = 2
dropout_rate = 0.5
scale_factor = 4 ** downsize_factor
lr_initial_d = 0.0002
lr_initial_g = 0.0002
lr_decay_steps = 1000
noise_dim = 128
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )<load_from_csv>
dog_features_data = tf.data.Dataset.from_tensor_slices(dog_features_tf ).shuffle(sample_size ).map(flip ).batch(batch_size )
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )<categorify>
weight_initializer = tf.keras.initializers.TruncatedNormal(stddev=weight_init_std, mean=weight_init_mean,
seed=42 )
def process_df(df_):
encode_data(df_)
 for f_ in ['v2a1', 'v18q1', 'meaneduc', 'SQBmeaned']:
df_[f_] = df_[f_].fillna(0)
 df_['rez_esc'] = df_['rez_esc'].fillna(-1)
 return do_features(df_)
 train = process_df(train)
 test = process_df(test )<concatenate>
class DenseSN(Dense):
def build(self, input_shape):
assert len(input_shape)>= 2
input_dim = input_shape[-1]
self.kernel = self.add_weight(shape=(input_dim, self.units),
initializer=self.kernel_initializer,
name='kernel',
regularizer=self.kernel_regularizer,
constraint=self.kernel_constraint)
 if self.use_bias:
self.bias = self.add_weight(shape=(self.units,),
initializer=self.bias_initializer,
name='bias',
regularizer=self.bias_regularizer,
constraint=self.bias_constraint)
 else:
self.bias = None
self.u = self.add_weight(shape=tuple([1, self.kernel.shape.as_list() [-1]]),
initializer=initializers.RandomNormal(0, 1),
name='sn',
trainable=False)
 self.input_spec = InputSpec(min_ndim=2, axes={-1: input_dim})
 self.built = True
def call(self, inputs, training=None):
def _l2normalize(v, eps=1e-12):
return v /(K.sum(v ** 2)** 0.5 + eps)
 def power_iteration(W, u):
_u = u
_v = _l2normalize(K.dot(_u, K.transpose(W)))
 _u = _l2normalize(K.dot(_v, W))
return _u, _v
W_shape = self.kernel.shape.as_list()
W_reshaped = K.reshape(self.kernel, [-1, W_shape[-1]])
 _u, _v = power_iteration(W_reshaped, self.u)
 sigma=K.dot(_v, W_reshaped)
 sigma=K.dot(sigma, K.transpose(_u))
W_bar = W_reshaped / sigma
if training in {0, False}:
W_bar = K.reshape(W_bar, W_shape)
 else:
with tf.control_dependencies([self.u.assign(_u)]):
W_bar = K.reshape(W_bar, W_shape)
 output = K.dot(inputs, W_bar)
 if self.use_bias:
output = K.bias_add(output, self.bias, data_format='channels_last')
 if self.activation is not None:
output = self.activation(output)
 return output
class ConvSN2D(Conv2D):
def build(self, input_shape):
if self.data_format == 'channels_first':
channel_axis = 1
else:
channel_axis = -1
if input_shape[channel_axis] is None:
raise ValueError('The channel dimension of the inputs '
'should be defined.Found `None`.')
 input_dim = input_shape[channel_axis]
kernel_shape = self.kernel_size +(input_dim, self.filters)
 self.kernel = self.add_weight(shape=kernel_shape,
initializer=self.kernel_initializer,
name='kernel',
regularizer=self.kernel_regularizer,
constraint=self.kernel_constraint)
 if self.use_bias:
self.bias = self.add_weight(shape=(self.filters,),
initializer=self.bias_initializer,
name='bias',
regularizer=self.bias_regularizer,
constraint=self.bias_constraint)
 else:
self.bias = None
self.u = self.add_weight(shape=tuple([1, self.kernel.shape.as_list() [-1]]),
initializer=initializers.RandomNormal(0, 1),
name='sn',
trainable=False)
 self.input_spec = InputSpec(ndim=self.rank + 2,
axes={channel_axis: input_dim})
 self.built = True
def call(self, inputs, training=None):
def _l2normalize(v, eps=1e-12):
return v /(K.sum(v ** 2)** 0.5 + eps)
 def power_iteration(W, u):
_u = u
_v = _l2normalize(K.dot(_u, K.transpose(W)))
 _u = _l2normalize(K.dot(_v, W))
return _u, _v
W_shape = self.kernel.shape.as_list()
W_reshaped = K.reshape(self.kernel, [-1, W_shape[-1]])
 _u, _v = power_iteration(W_reshaped, self.u)
 sigma=K.dot(_v, W_reshaped)
 sigma=K.dot(sigma, K.transpose(_u))
W_bar = W_reshaped / sigma
if training in {0, False}:
W_bar = K.reshape(W_bar, W_shape)
 else:
with tf.control_dependencies([self.u.assign(_u)]):
W_bar = K.reshape(W_bar, W_shape)
 outputs = K.conv2d(
 inputs,
W_bar,
strides=self.strides,
padding=self.padding,
data_format=self.data_format,
dilation_rate=self.dilation_rate)
 if self.use_bias:
outputs = K.bias_add(
 outputs,
self.bias,
data_format=self.data_format)
 if self.activation is not None:
return self.activation(outputs)
 return outputs
class ConvSN2DTranspose(Conv2DTranspose):
def build(self, input_shape):
if len(input_shape)!= 4:
raise ValueError('Inputs should have rank ' +
str(4)+
'; Received input shape:', str(input_shape))
if self.data_format == 'channels_first':
channel_axis = 1
else:
channel_axis = -1
if input_shape[channel_axis] is None:
raise ValueError('The channel dimension of the inputs '
'should be defined.Found `None`.')
 input_dim = input_shape[channel_axis]
kernel_shape = self.kernel_size +(self.filters, input_dim)
 self.kernel = self.add_weight(shape=kernel_shape,
initializer=self.kernel_initializer,
name='kernel',
regularizer=self.kernel_regularizer,
constraint=self.kernel_constraint)
 if self.use_bias:
self.bias = self.add_weight(shape=(self.filters,),
initializer=self.bias_initializer,
name='bias',
regularizer=self.bias_regularizer,
constraint=self.bias_constraint)
 else:
self.bias = None
self.u = self.add_weight(shape=tuple([1, self.kernel.shape.as_list() [-1]]),
initializer=initializers.RandomNormal(0, 1),
name='sn',
trainable=False)
 self.input_spec = InputSpec(ndim=4, axes={channel_axis: input_dim})
 self.built = True
def call(self, inputs):
input_shape = K.shape(inputs)
 batch_size = input_shape[0]
if self.data_format == 'channels_first':
h_axis, w_axis = 2, 3
else:
h_axis, w_axis = 1, 2
height, width = input_shape[h_axis], input_shape[w_axis]
kernel_h, kernel_w = self.kernel_size
stride_h, stride_w = self.strides
if self.output_padding is None:
out_pad_h = out_pad_w = None
else:
out_pad_h, out_pad_w = self.output_padding
out_height = conv_utils.deconv_length(height,
stride_h, kernel_h,
self.padding,
out_pad_h)
 out_width = conv_utils.deconv_length(width,
stride_w, kernel_w,
self.padding,
out_pad_w)
 if self.data_format == 'channels_first':
output_shape =(batch_size, self.filters, out_height, out_width)
 else:
output_shape =(batch_size, out_height, out_width, self.filters)
 def _l2normalize(v, eps=1e-12):
return v /(K.sum(v ** 2)** 0.5 + eps)
 def power_iteration(W, u):
_u = u
_v = _l2normalize(K.dot(_u, K.transpose(W)))
 _u = _l2normalize(K.dot(_v, W))
return _u, _v
W_shape = self.kernel.shape.as_list()
W_reshaped = K.reshape(self.kernel, [-1, W_shape[-1]])
 _u, _v = power_iteration(W_reshaped, self.u)
 sigma=K.dot(_v, W_reshaped)
 sigma=K.dot(sigma, K.transpose(_u))
W_bar = W_reshaped / sigma
if training in {0, False}:
W_bar = K.reshape(W_bar, W_shape)
 else:
with tf.control_dependencies([self.u.assign(_u)]):
W_bar = K.reshape(W_bar, W_shape)
 self.kernel = W_bar
outputs = K.conv2d_transpose(
 inputs,
self.kernel,
output_shape,
self.strides,
padding=self.padding,
data_format=self.data_format)
 if self.use_bias:
outputs = K.bias_add(
 outputs,
self.bias,
data_format=self.data_format)
 if self.activation is not None:
return self.activation(outputs)
 return outputs
def train_test_apply_func(train_, test_, func_):
test_['Target'] = 0
xx = pd.concat([train_, test_])
 xx_func = func_(xx)
 train_ = xx_func.iloc[:train_.shape[0], :]
test_ = xx_func.iloc[train_.shape[0]:, :].drop('Target', axis=1)
 del xx, xx_func
return train_, test_<feature_engineering>
def transposed_conv(model, out_channels, ksize, stride_size, ptype='same'):
model.add(Conv2DTranspose(out_channels,(ksize, ksize),
strides=(stride_size, stride_size), padding=ptype,
kernel_initializer=weight_initializer, use_bias=False))
model.add(BatchNormalization())
 model.add(ReLU())
 return model
def transposed_convSN(model, out_channels, ksize, stride_size, ptype='same'):
model.add(ConvSN2DTranspose(out_channels,(ksize, ksize),
strides=(stride_size, stride_size), padding=ptype,
kernel_initializer=weight_initializer, use_bias=False))
model.add(BatchNormalization())
 model.add(ReLU())
 return model
def convSN(model, out_channels, ksize, stride_size):
model.add(ConvSN2D(out_channels,(ksize, ksize), strides=(stride_size, stride_size), padding='same',
kernel_initializer=weight_initializer, use_bias=False))
model.add(BatchNormalization())
 model.add(LeakyReLU(alpha=leaky_relu_slope))
return model
def conv(model, out_channels, ksize, stride_size):
model.add(Conv2D(out_channels,(ksize, ksize), strides=(stride_size, stride_size), padding='same',
kernel_initializer=weight_initializer, use_bias=False))
model.add(BatchNormalization())
 model.add(LeakyReLU(alpha=leaky_relu_slope))
return model
train, test = train_test_apply_func(train, test, convert_OHE2LE )<categorify>
print(image_height // scale_factor, image_width // scale_factor, 512 )
cols_2_ohe = ['eviv_LE', 'etecho_LE', 'epared_LE', 'elimbasu_LE',
'energcocinar_LE', 'sanitario_LE', 'manual_elec_LE',
'pared_LE']
cols_nums = ['age', 'meaneduc', 'dependency',
'hogar_nin', 'hogar_adul', 'hogar_mayor', 'hogar_total',
'bedrooms', 'overcrowding']
def convert_geo2aggs(df_):
tmp_df = pd.concat([df_[(['lugar_LE', 'idhogar']+cols_nums)],
pd.get_dummies(df_[cols_2_ohe],
columns=cols_2_ohe)],axis=1)
 geo_agg = tmp_df.groupby(['lugar_LE','idhogar'] ).mean().groupby('lugar_LE' ).mean().astype(np.float32)
 geo_agg.columns = pd.Index(['geo_' + e for e in geo_agg.columns.tolist() ])
 del tmp_df
return df_.join(geo_agg, how='left', on='lugar_LE')
<prepare_x_and_y>
def DogGenerator() :
model = Sequential()
model.add(Dense(image_width // scale_factor * image_height // scale_factor * 128,
input_shape=(noise_dim,), kernel_initializer=weight_initializer))
model.add(Reshape(( image_height // scale_factor, image_width // scale_factor, 128)))
 model = transposed_conv(model, 512, ksize=5, stride_size=1)
 model.add(Dropout(dropout_rate))
model = transposed_conv(model, 256, ksize=5, stride_size=2)
 model.add(Dropout(dropout_rate))
model = transposed_conv(model, 128, ksize=5, stride_size=2)
 model = transposed_conv(model, 64, ksize=5, stride_size=2)
 model = transposed_conv(model, 32, ksize=5, stride_size=2)
 model.add(Dense(3, activation='tanh', kernel_initializer=weight_initializer))
return model
X = train.query('parentesco1==1')
 y = X['Target'] - 1
X = X.drop(['Target'], axis=1 )<define_variables>
def generate_latent_points(latent_dim, n_samples):
x_input = np.random.randn(latent_dim * n_samples)
 x_input = x_input.reshape(( n_samples, latent_dim))
return x_input
cols_2_drop = ['agg18_estadocivil1_MEAN', 'agg18_estadocivil3_COUNT', 'agg18_estadocivil4_COUNT', 'agg18_estadocivil5_COUNT', 'agg18_estadocivil6_COUNT', 'agg18_estadocivil7_COUNT', 'agg18_instlevel1_COUNT', 'agg18_instlevel2_COUNT', 'agg18_instlevel3_COUNT', 'agg18_instlevel4_COUNT', 'agg18_instlevel5_COUNT', 'agg18_instlevel6_COUNT', 'agg18_instlevel7_COUNT', 'agg18_instlevel8_COUNT', 'agg18_instlevel9_COUNT', 'agg18_parentesco10_COUNT', 'agg18_parentesco10_MEAN', 'agg18_parentesco11_COUNT', 'agg18_parentesco11_MEAN', 'agg18_parentesco12_COUNT', 'agg18_parentesco12_MEAN', 'agg18_parentesco1_COUNT', 'agg18_parentesco2_COUNT', 'agg18_parentesco3_COUNT', 'agg18_parentesco4_COUNT', 'agg18_parentesco4_MEAN', 'agg18_parentesco5_COUNT', 'agg18_parentesco6_COUNT', 'agg18_parentesco6_MEAN', 'agg18_parentesco7_COUNT', 'agg18_parentesco7_MEAN', 'agg18_parentesco8_COUNT', 'agg18_parentesco8_MEAN', 'agg18_parentesco9_COUNT', 'fe_people_weird_stat', 'hacapo', 'hacdor', 'mobilephone', 'parentesco1', 'parentesco_LE', 'rez_esc', 'v14a', 'v18q']
X.drop(( cols_2_drop+['idhogar']), axis=1, inplace=True)
 test.drop(( cols_2_drop+['idhogar']), axis=1, inplace=True )<split>
def DogDiscriminator(spectral_normalization=True):
model = Sequential()
if spectral_normalization:
model.add(ConvSN2D(64,(5, 5), strides=(1,1), padding='same', use_bias=False,
input_shape=[image_height, image_width, image_channels],
kernel_initializer=weight_initializer))
model.add(LeakyReLU(alpha=leaky_relu_slope))
model = convSN(model, 64, ksize=5, stride_size=2)
 model = convSN(model, 128, ksize=5, stride_size=2)
 model = convSN(model, 256, ksize=5, stride_size=2)
 model.add(Flatten())
 model.add(DenseSN(1, activation='sigmoid'))
else:
model.add(Conv2D(64,(4, 4), strides=(2,2), padding='same', use_bias=False,
input_shape=[image_height, image_width, image_channels],
kernel_initializer=weight_initializer))
model.add(LeakyReLU(alpha=leaky_relu_slope))
model = conv(model, 64, ksize=4, stride_size=2)
 model = conv(model, 128, ksize=4, stride_size=2)
 model = conv(model, 256, ksize=4, stride_size=2)
 model.add(Flatten())
 model.add(Dense(1, activation='sigmoid'))
return model
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=42, stratify=y )<train_model>
decision = dog_discriminator(generated_image)
 print(decision )
<compute_train_metric>
def smooth_positive_labels(y):
return y - 0.3 +(np.random.random(y.shape)* 0.5)
 def smooth_negative_labels(y):
return y + np.random.random(y.shape)* 0.3
def evaluate_macroF1_lgb(truth, predictions):
pred_labels = predictions.reshape(len(np.unique(truth)) ,-1 ).argmax(axis=0)
 f1 = f1_score(truth, pred_labels, average='macro')
 return('macroF1', f1, True)
 fit_params={"early_stopping_rounds":300,
"eval_metric" : evaluate_macroF1_lgb,
"eval_set" : [(X_train,y_train),(X_test,y_test)],
'eval_names': ['train', 'valid'],
'verbose': False,
'categorical_feature': 'auto'}
def learning_rate_power_0997(current_iter):
base_learning_rate = 0.1
min_learning_rate = 0.02
lr = base_learning_rate * np.power (.995, current_iter)
 return max(lr, min_learning_rate)
 fit_params['verbose'] = 200
fit_params['callbacks'] = [lgb.reset_parameter(learning_rate=learning_rate_power_0997)]<init_hyperparams>
def noisy_labels(y, p_flip):
n_select = int(p_flip * int(y.shape[0]))
flip_ix = np.random.choice([i for i in range(int(y.shape[0])) ], size=n_select)
 op_list = []
for i in range(int(y.shape[0])) :
if i in flip_ix:
op_list.append(tf.subtract(1, y[i]))
else:
op_list.append(y[i])
 outputs = tf.stack(op_list)
 return outputs
opt_parameters = {
'bagging_fraction': 0.85,
'colsample_bytree': 1.0,
'feature_fraction': 0.1,
'lambda_l1': 5.0,
'lambda_l2': 3.0,
'max_depth': 9,
'min_child_samples': 55,
'min_child_weight': 5.0,
'min_split_gain': 0.1,
'num_leaves': 45,
'subsample': 0.84,
'min_sum_hessian_in_leaf': 1,
'importance_type': 'gain'
} 
<train_model>
generator_optimizer = tf.train.AdamOptimizer(learning_rate=lr_initial_g, beta1=0.5)
 discriminator_optimizer = tf.train.AdamOptimizer(learning_rate=lr_initial_d, beta1=0.5)
 cross_entropy = tf.keras.losses.BinaryCrossentropy(from_logits=False )
def _parallel_fit_estimator(estimator, X, y, sample_weight=None, **fit_params):

 if sample_weight is not None:
estimator.fit(X, y, sample_weight=sample_weight, **fit_params)
 else:
estimator.fit(X, y, **fit_params)
 return estimator
class VotingClassifierLGBM(VotingClassifier):

 def fit(self, X, y, sample_weight=None, **fit_params):
if isinstance(y, np.ndarray)and len(y.shape)> 1 and y.shape[1] > 1:
raise NotImplementedError('Multilabel and multi-output'
' classification is not supported.')
 if self.voting not in('soft', 'hard'):
raise ValueError("Voting must be 'soft' or 'hard'; got(voting=%r)"
% self.voting)
 if self.estimators is None or len(self.estimators)== 0:
raise AttributeError('Invalid `estimators` attribute, `estimators`'
' should be a list of(string, estimator)'
' tuples')
 if(self.weights is not None and
len(self.weights)!= len(self.estimators)) :
raise ValueError('Number of classifiers and weights must be equal'
'; got %d weights, %d estimators'
%(len(self.weights), len(self.estimators)))
 if sample_weight is not None:
for name, step in self.estimators:
if not has_fit_parameter(step, 'sample_weight'):
raise ValueError('Underlying estimator '%s' does not'
' support sample weights.' % name)
 names, clfs = zip(*self.estimators)
 self._validate_names(names)
 n_isnone = np.sum([clf is None for _, clf in self.estimators])
 if n_isnone == len(self.estimators):
raise ValueError('All estimators are None.At least one is '
'required to be a classifier!')
 self.le_ = LabelEncoder().fit(y)
 self.classes_ = self.le_.classes_
self.estimators_ = []
transformed_y = self.le_.transform(y)
 self.estimators_ = Parallel(n_jobs=self.n_jobs )(
 delayed(_parallel_fit_estimator )(clone(clf), X, transformed_y,
sample_weight=sample_weight, **fit_params)
 for clf in clfs if clf is not None)
 return self<train_model>
def discriminator_loss(real_output, fake_output, loss_func, apply_label_smoothing=True, label_noise=True):
if label_noise and apply_label_smoothing:
real_output_noise = noisy_labels(tf.ones_like(real_output), 0.05)
 fake_output_noise = noisy_labels(tf.zeros_like(fake_output), 0.05)
 real_output_smooth = smooth_positive_labels(real_output_noise)
 fake_output_smooth = smooth_negative_labels(fake_output_noise)
 if loss_func == 'gan':
real_loss = cross_entropy(tf.ones_like(real_output_smooth), real_output)
 fake_loss = cross_entropy(tf.zeros_like(fake_output_smooth), fake_output)
 else:
if loss_func == 'ralsgan':
return(tf.reduce_mean(tf.square(real_output_smooth - tf.reduce_mean(fake_output_smooth)- tf.ones_like(real_output_smooth)))
 + tf.reduce_mean(tf.square(fake_output_smooth - tf.reduce_mean(real_output_smooth)+ tf.ones_like(fake_output_smooth)))) / 2.
 elif loss_func == 'rasgan':
avg_fake_logit = tf.reduce_mean(fake_output_smooth)
 avg_real_logit = tf.reduce_mean(real_output_smooth)
 D_r_tilde = tf.nn.sigmoid(real_output_smooth - avg_fake_logit)
 D_f_tilde = tf.nn.sigmoid(fake_output_smooth - avg_real_logit)
 total_loss = - tf.reduce_mean(tf.log(
 D_r_tilde + 1e-14)) - tf.reduce_mean(tf.log(1 - D_f_tilde + 1e-14))
return total_loss
elif loss_func == 'rahinge':
real_loss = tf.reduce_mean(tf.nn.relu(tf.ones_like(real_output_smooth)-(real_output_smooth - tf.reduce_mean(fake_output_smooth))))
fake_loss = tf.reduce_mean(tf.nn.relu(tf.ones_like(fake_output_smooth)+(fake_output_smooth - tf.reduce_mean(real_output_smooth))))
total_loss = real_loss + fake_loss
return total_loss
elif label_noise and not apply_label_smoothing:
real_output_noise = noisy_labels(tf.ones_like(real_output), 0.05)
 fake_output_noise = noisy_labels(tf.zeros_like(fake_output), 0.05)
 if loss_func == 'gan':
real_loss = cross_entropy(tf.ones_like(real_output_noise), real_output)
 fake_loss = cross_entropy(tf.zeros_like(fake_output_noise), fake_output)
 else:
if loss_func == 'ralsgan':
return(tf.reduce_mean(tf.square(real_output_noise - tf.reduce_mean(fake_output_noise)- tf.ones_like(real_output_noise)))
 + tf.reduce_mean(tf.square(fake_output_noise - tf.reduce_mean(real_output_noise)+ tf.ones_like(fake_output_noise)))) / 2.
 elif loss_func == 'rasgan':
avg_fake_logit = tf.reduce_mean(fake_output_noise)
 avg_real_logit = tf.reduce_mean(real_output_noise)
 D_r_tilde = tf.nn.sigmoid(real_output_noise - avg_fake_logit)
 D_f_tilde = tf.nn.sigmoid(fake_output_noise - avg_real_logit)
 total_loss = - tf.reduce_mean(tf.log(
 D_r_tilde + 1e-14)) - tf.reduce_mean(tf.log(1 - D_f_tilde + 1e-14))
return total_loss
elif loss_func == 'rahinge':
real_loss = tf.reduce_mean(tf.nn.relu(tf.ones_like(real_output_noise)-(real_output_noise - tf.reduce_mean(fake_output_noise))))
fake_loss = tf.reduce_mean(tf.nn.relu(tf.ones_like(fake_output_noise)+(fake_output_noise - tf.reduce_mean(real_output_noise))))
total_loss = real_loss + fake_loss
return total_loss
elif apply_label_smoothing and not label_noise:
real_output_smooth = smooth_positive_labels(tf.ones_like(real_output))
fake_output_smooth = smooth_negative_labels(tf.zeros_like(fake_output))
if loss_func == 'gan':
real_loss = cross_entropy(tf.ones_like(real_output_smooth), real_output)
 fake_loss = cross_entropy(tf.zeros_like(fake_output_smooth), fake_output)
 else:
if loss_func == 'ralsgan':
return(tf.reduce_mean(tf.square(real_output_smooth - tf.reduce_mean(fake_output_smooth)- tf.ones_like(real_output_smooth)))
 + tf.reduce_mean(tf.square(fake_output_smooth - tf.reduce_mean(real_output_smooth)+ tf.ones_like(fake_output_smooth)))) / 2.
 elif loss_func == 'rasgan':
avg_fake_logit = tf.reduce_mean(fake_output_smooth)
 avg_real_logit = tf.reduce_mean(real_output_smooth)
 D_r_tilde = tf.nn.sigmoid(real_output_smooth - avg_fake_logit)
 D_f_tilde = tf.nn.sigmoid(fake_output_smooth - avg_real_logit)
 total_loss = - tf.reduce_mean(tf.log(
 D_r_tilde + 1e-14)) - tf.reduce_mean(tf.log(1 - D_f_tilde + 1e-14))
return total_loss
elif loss_func == 'rahinge':
real_loss = tf.reduce_mean(tf.nn.relu(tf.ones_like(real_output_smooth)-(real_output_smooth - tf.reduce_mean(fake_output_smooth))))
fake_loss = tf.reduce_mean(tf.nn.relu(tf.ones_like(fake_output_smooth)+(fake_output_smooth - tf.reduce_mean(real_output_smooth))))
total_loss = real_loss + fake_loss
return total_loss
else:
if loss_func == 'gan':
real_loss = cross_entropy(tf.ones_like(real_output), real_output)
 fake_loss = cross_entropy(tf.zeros_like(fake_output), fake_output)
 else:
if loss_func == 'ralsgan':
return(tf.reduce_mean(tf.square(real_output - tf.reduce_mean(fake_output)- tf.ones_like(real_output)))
 + tf.reduce_mean(tf.square(fake_output - tf.reduce_mean(real_output)+ tf.ones_like(fake_output)))) / 2.
 elif loss_func == 'rasgan':
avg_fake_logit = tf.reduce_mean(fake_output)
 avg_real_logit = tf.reduce_mean(real_output)
 D_r_tilde = tf.nn.sigmoid(real_output - avg_fake_logit)
 D_f_tilde = tf.nn.sigmoid(fake_output - avg_real_logit)
 total_loss = - tf.reduce_mean(tf.log(
 D_r_tilde + 1e-14)) - tf.reduce_mean(tf.log(1 - D_f_tilde + 1e-14))
return total_loss
elif loss_func == 'rahinge':
real_loss = tf.reduce_mean(tf.nn.relu(tf.ones_like(real_output)-(real_output - tf.reduce_mean(fake_output))))
fake_loss = tf.reduce_mean(tf.nn.relu(tf.ones_like(fake_output)+(fake_output - tf.reduce_mean(real_output))))
total_loss = real_loss + fake_loss
return total_loss
clfs = []
for i in range(20):
clf = lgb.LGBMClassifier(max_depth=-1, learning_rate=0.1, objective='multiclass',
random_state=314+i, silent=True, metric='None',
n_jobs=4, n_estimators=5000, class_weight='balanced')
 clf.set_params(**opt_parameters)
 clfs.append(( 'lgbm{}'.format(i), clf))
vc = VotingClassifierLGBM(clfs, voting='soft')
 del clfs
_ = vc.fit(X_train, y_train, **fit_params)
 clf_final = vc.estimators_[0]<compute_train_metric>
def generator_loss(real_output, fake_output, loss_func, apply_label_smoothing=True):
if apply_label_smoothing:
fake_output_smooth = smooth_negative_labels(tf.ones_like(fake_output))
if loss_func == 'gan':
return cross_entropy(tf.ones_like(fake_output_smooth), fake_output)
 else:
if loss_func == 'ralsgan':
return(tf.reduce_mean(tf.square(real_output - tf.reduce_mean(fake_output_smooth)+ tf.ones_like(real_output)))
 + tf.reduce_mean(tf.square(fake_output_smooth - tf.reduce_mean(real_output)- tf.ones_like(fake_output_smooth)))) / 2.
 elif loss_func == 'rasgan':
avg_fake_logit = tf.reduce_mean(fake_output_smooth)
 avg_real_logit = tf.reduce_mean(real_output)
 D_r_tilde = tf.nn.sigmoid(real_output - avg_fake_logit)
 D_f_tilde = tf.nn.sigmoid(fake_output_smooth - avg_real_logit)
 total_loss = - tf.reduce_mean(tf.log(
 D_f_tilde + 1e-14)) - tf.reduce_mean(tf.log(1 - D_r_tilde + 1e-14))
return total_loss
elif loss_func == 'rahinge':
fake_loss = tf.reduce_mean(tf.nn.relu(tf.ones_like(fake_output_smooth)-(fake_output_smooth - tf.reduce_mean(real_output))))
real_loss = tf.reduce_mean(tf.nn.relu(tf.ones_like(real_output)+(real_output - tf.reduce_mean(fake_output_smooth))))
loss = fake_loss + real_loss
return loss
else:
if loss_func == 'gan':
return cross_entropy(tf.ones_like(fake_output), fake_output)
 else:
if loss_func == 'ralsgan':
return(tf.reduce_mean(tf.square(real_output - tf.reduce_mean(fake_output)+ tf.ones_like(real_output)))
 + tf.reduce_mean(tf.square(fake_output - tf.reduce_mean(real_output)- tf.ones_like(fake_output)))) / 2.
 elif loss_func == 'rasgan':
avg_fake_logit = tf.reduce_mean(fake_output)
 avg_real_logit = tf.reduce_mean(real_output)
 D_r_tilde = tf.nn.sigmoid(real_output - avg_fake_logit)
 D_f_tilde = tf.nn.sigmoid(fake_output - avg_real_logit)
 total_loss = - tf.reduce_mean(tf.log(
 D_f_tilde + 1e-14)) - tf.reduce_mean(tf.log(1 - D_r_tilde + 1e-14))
return total_loss
elif loss_func == 'rahinge':
fake_loss = tf.reduce_mean(tf.nn.relu(tf.ones_like(fake_output)-(fake_output - tf.reduce_mean(real_output))))
real_loss = tf.reduce_mean(tf.nn.relu(tf.ones_like(real_output)+(real_output - tf.reduce_mean(fake_output))))
loss = fake_loss + real_loss
return loss
global_score = f1_score(y_test, clf_final.predict(X_test), average='macro')
 vc.voting = 'soft'
global_score_soft = f1_score(y_test, vc.predict(X_test), average='macro')
 vc.voting = 'hard'
global_score_hard = f1_score(y_test, vc.predict(X_test), average='macro')
 print('Validation score of a single LGBM Classifier: {:.4f}'.format(global_score))
print('Validation score of a VotingClassifier on 3 LGBMs with soft voting strategy: {:.4f}'.format(global_score_soft))
print('Validation score of a VotingClassifier on 3 LGBMs with hard voting strategy: {:.4f}'.format(global_score_hard)) 
<load_from_csv>
checkpoint_dir = '/training_checkpoints'
checkpoint_prefix = os.path.join(checkpoint_dir, "ckpt")
 checkpoint = tf.train.Checkpoint(generator_optimizer=generator_optimizer,
discriminator_optimizer=discriminator_optimizer,
generator=dog_generator,
discriminator=dog_discriminator )
y_subm = pd.read_csv('.. /input/sample_submission.csv' )<predict_on_test>
EPOCHS = 330
num_examples_to_generate = 16
seed = tf.random.normal([num_examples_to_generate, noise_dim] )
y_subm['Target'] = clf_final.predict(test)+ 1
vc.voting = 'soft'
y_subm_soft = y_subm.copy(deep=True)
 y_subm_soft['Target'] = vc.predict(test)+ 1
vc.voting = 'hard'
y_subm_hard = y_subm.copy(deep=True)
 y_subm_hard['Target'] = vc.predict(test)+ 1<create_dataframe>
def train_step(images, G_loss, D_loss, loss_type='gan'):
noise = tf.random.normal([batch_size, noise_dim])
 with tf.GradientTape() as gen_tape, tf.GradientTape() as disc_tape:
generated_images = dog_generator(noise, training=True)
 real_output = dog_discriminator(images, training=True)
 fake_output = dog_discriminator(generated_images, training=True)
 gen_loss = generator_loss(real_output, fake_output, loss_type, apply_label_smoothing=True)
 disc_loss = discriminator_loss(real_output, fake_output, loss_type,
apply_label_smoothing=True, label_noise=True)
 G_loss.append(gen_loss.numpy())
 D_loss.append(disc_loss.numpy())
 gradients_of_generator = gen_tape.gradient(gen_loss, dog_generator.trainable_variables)
 gradients_of_discriminator = disc_tape.gradient(disc_loss, dog_discriminator.trainable_variables)
 generator_optimizer.apply_gradients(zip(gradients_of_generator, dog_generator.trainable_variables))
discriminator_optimizer.apply_gradients(zip(gradients_of_discriminator, dog_discriminator.trainable_variables))
y_subm_0forNonHeads = y_subm.copy(deep=True )<save_to_csv>
class KernelEvalException(Exception):
pass
@dataclass
class MiFIDEvaluator(object):
model_path: str
train_images_path: str
feature_path: str = None
imsize: int = 64
output_layer: str = 'Pretrained_Net/pool_3:0'
input_layer: str = 'Pretrained_Net/ExpandDims:0'
output_shape: int = 2048
cosine_distance_eps: float = 0.1
batch_size: int = 50
fid_epsilon: float = 1e-14
def __post_init__(self):
tf.reset_default_graph()
self.create_model_graph()
with tf.Session() as sess:
if self.feature_path is None:
self.mu2, self.sigma2, self.features2 = self._handle_path_memorization(
 self.train_images_path, sess, is_checksize=False, is_check_png=False)
 else:
with np.load(self.feature_path)as f:
self.mu2, self.sigma2, self.features2 = f['m'], f['s'], f['features']
def create_model_graph(self):
with tf.gfile.FastGFile(self.model_path, 'rb')as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
 _ = tf.import_graph_def(graph_def, name='Pretrained_Net')
 def img_read_checks(self, filename, is_checksize=False, is_check_png=False):
im = Image.open(str(filename))
if is_checksize and im.size !=(self.imsize, self.imsize):
raise KernelEvalException(f'The images are not of size {check_imsize}')
 if is_check_png and im.format != 'PNG':
raise KernelEvalException('Only PNG images should be submitted.')
 if self.imsize is None:
return im
else:
return im.resize(( self.imsize, self.imsize), Image.ANTIALIAS)
 def _get_model_layer(self, sess):
layer = sess.graph.get_tensor_by_name(self.output_layer)
 ops = layer.graph.get_operations()
for op_idx, op in enumerate(ops):
for o in op.outputs:
shape = o.get_shape()
if shape._dims != []:
shape = [s.value for s in shape]
new_shape = []
for j, s in enumerate(shape):
if s == 1 and j == 0:
new_shape.append(None)
 else:
new_shape.append(s)
 o.__dict__['_shape_val'] = tf.TensorShape(new_shape)
 return layer
def get_activations(self, images, sess):
inception_layer = self._get_model_layer(sess)
 n_images = images.shape[0]
if self.batch_size > n_images:
warnings.warn('batch size is bigger than the data size.setting batch size to data size')
 self.batch_size = n_images
n_batches = n_images // self.batch_size + 1
pred_arr = np.empty(( n_images, self.output_shape))
for i in range(n_batches):
start = i * self.batch_size
if start + self.batch_size < n_images:
end = start + self.batch_size
else:
end = n_images
batch = images[start:end]
pred = sess.run(inception_layer, {self.input_layer: batch})
 pred_arr[start:end] = pred.reshape(-1, self.output_shape)
 return pred_arr
def calculate_activation_statistics(self, images, sess):
act = self.get_activations(images, sess)
 mu = np.mean(act, axis=0)
 sigma = np.cov(act, rowvar=False)
 return mu, sigma, act
def _handle_path_memorization(self, path, sess, is_checksize, is_check_png):
path = Path(path)
 files = list(path.glob('*.jpg')) + list(path.glob('*.png'))
x = np.array([np.array(self.img_read_checks(fn, is_checksize, is_check_png)) for fn in files])
 m, s, features = self.calculate_activation_statistics(x, sess)
 del x
return m, s, features
def calculate_frechet_distance(self, mu1, sigma1):
mu1 = np.atleast_1d(mu1)
 mu2 = np.atleast_1d(self.mu2)
 sigma1 = np.atleast_2d(sigma1)
 sigma2 = np.atleast_2d(self.sigma2)
 assert mu1.shape == mu2.shape, 'Training and test mean vectors have different lengths'
assert sigma1.shape == sigma2.shape, 'Training and test covariances have different dimensions'
covmean, _ = linalg.sqrtm(sigma1.dot(sigma2), disp=False)
 if not np.isfinite(covmean ).all() :
msg = f'fid calculation produces singular product; adding {self.eps} to diagonal of cov estimates'
warnings.warn(msg)
 offset = np.eye(sigma1.shape[0])* self.eps
covmean = linalg.sqrtm(( sigma1 + offset ).dot(sigma2 + offset))
if np.iscomplexobj(covmean):
if not np.allclose(np.diagonal(covmean ).imag, 0, atol=1e-3):
m = np.max(np.abs(covmean.imag))
raise ValueError(f'Imaginary component {m}')
 covmean = covmean.real
tr_covmean = np.trace(covmean)
 return(mu1 - mu2 ).dot(mu1 - mu2)+ np.trace(sigma1)+ np.trace(sigma2)- 2 * tr_covmean
@staticmethod
def normalize_rows(x):
return np.nan_to_num(x / np.linalg.norm(x, ord=2, axis=1, keepdims=True))
def cosine_distance(self, features1):
features1_nozero = features1[np.sum(features1, axis=1)!= 0]
features2_nozero = self.features2[np.sum(self.features2, axis=1)!= 0]
norm_f1 = self.normalize_rows(features1_nozero)
 norm_f2 = self.normalize_rows(features2_nozero)
 d = 1.0 - np.abs(np.matmul(norm_f1, norm_f2.T))
mean_min_d = np.mean(np.min(d, axis=1))
return mean_min_d
def calculate_kid_given_paths(self, user_images_unzipped_path):
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
 m1, s1, features1 = self._handle_path_memorization(
 user_images_unzipped_path, sess, is_checksize=True, is_check_png=True)
 fid_value = self.calculate_frechet_distance(m1, s1)
 distance = self.cosine_distance(features1)
 return fid_value, distance
def distance_thresholding(self, d):
if d < self.cosine_distance_eps:
return d
else:
return 1
def evaluate(self, user_images_unzipped_path):
fid_value, distance = self.calculate_kid_given_paths(user_images_unzipped_path)
 distance = self.distance_thresholding(distance)
 return fid_value, distance, fid_value /(distance + self.fid_epsilon )
now = datetime.now()
sub_file = 'submission_LGB_{:.4f}_{}.csv'.format(global_score, str(now.strftime('%Y-%m-%d-%H-%M')))
 sub_file_soft = 'submission_soft_LGB_{:.4f}_{}.csv'.format(global_score_soft, str(now.strftime('%Y-%m-%d-%H-%M')))
 sub_file_hard = 'submission_hard_LGB_{:.4f}_{}.csv'.format(global_score_hard, str(now.strftime('%Y-%m-%d-%H-%M')))
 sub_file_0forNonHeads = 'submission_0forNonHead_LGB_{:.4f}_{}.csv'.format(global_score, str(now.strftime('%Y-%m-%d-%H-%M')))
 y_subm.to_csv(sub_file, index=False)
 y_subm_soft.to_csv(sub_file_soft, index=False)
 y_subm_hard.to_csv(sub_file_hard, index=False)
 y_subm_0forNonHeads.to_csv(sub_file_0forNonHeads, index=False )<import_modules>
def zip_images(filename='images.zip'):
z = zipfile.PyZipFile(filename, mode='w')
 for k in range(image_sample_size):
generated_image = dog_generator(tf.random.normal([1, noise_dim]), training=False)
 f = str(k)+'.png'
img = np.array(generated_image)
 img =(img[0, :, :, :] + 1.) / 2.
 img = Image.fromarray(( 255*img ).astype('uint8' ).reshape(( image_height,image_width,image_channels)))
 img.save(f,'PNG')
 z.write(f)
 os.remove(f)
 z.close()
print('Saved final images for submission.')
 def save_images(directory=OUT_DIR):
for k in range(image_sample_size):
generated_image = dog_generator(tf.random.normal([1, noise_dim]), training=False)
 f = str(k)+'.png'
f = os.path.join(directory, f)
 img = np.array(generated_image)
 img =(img[0, :, :, :] + 1.) / 2.
 img = Image.fromarray(( 255*img ).astype('uint8' ).reshape(( image_height,image_width,image_channels)))
 img.save(f,'PNG')
 print('Saved temporary images for evaluation.' )
%matplotlib inline
print(os.listdir(".. /input")) 
<define_variables>
def train(dataset, epochs):
G_loss = []
D_loss = []
for epoch in tqdm(range(epochs)) :
start = time.time()
new_lr_d = lr_initial_d
new_lr_g = lr_initial_g
global_step = 0
for image_batch in dataset:
train_step(image_batch, G_loss, D_loss)
 global_step = global_step + 1
if(epoch + 1)% 100 == 0:
plot_loss(G_loss, D_loss, epoch + 1)
 generate_and_save_images(dog_generator, epoch + 1, seed)

if(epoch + 1)% 200 == 0:
print('Reducing learning rate.. ')
 new_lr_d = tf.train.cosine_decay(new_lr_d, min(global_step, lr_decay_steps), lr_decay_steps)
 new_lr_g = tf.train.cosine_decay(new_lr_g, min(global_step, lr_decay_steps), lr_decay_steps)
 generator_optimizer = tf.train.AdamOptimizer(learning_rate=new_lr_d, beta1=0.5)
 discriminator_optimizer = tf.train.AdamOptimizer(learning_rate=new_lr_g, beta1=0.5)
 G_loss = []
D_loss = []
print('Epoch: {} computed for {} sec'.format(epoch + 1, time.time() - start))
generate_and_save_images(dog_generator, epochs, seed)
 checkpoint.save(file_prefix = checkpoint_prefix)

print('Final epoch.' )
def dprint(*args, **kwargs):
print("[{}] ".format(datetime.datetime.now().strftime("%Y-%m-%d %H:%M")) + \
" ".join(map(str,args)) , **kwargs)
 id_name = 'Id'
target_name = 'Target' 
<load_from_csv>
train(dog_features_data, EPOCHS )
<concatenate><EOS> 
zip_images()