Kaggle competition: TMDB Box Office Prediction
<SOS> metric: RMSLE Kaggle data source: tmdb-box-office-prediction<normalization>
sns.set()
train_orig = pd.read_csv('.. /input/train.csv')
 test_orig = pd.read_csv('.. /input/test.csv')
 subm = pd.DataFrame()
subm['id'] = test_orig.id.values
def truncated_normal_(tensor, mean=0, std=1):
size = tensor.shape
tmp = tensor.new_empty(size +(4,)).normal_()
valid =(tmp < 2)&(tmp > -2)
 ind = valid.max(-1, keepdim=True)[1]
tensor.data.copy_(tmp.gather(-1, ind ).squeeze(-1))
tensor.data.mul_(std ).add_(mean)
 return tensor<normalization>
train_orig['bool_belongs_to_collection'] =(train_orig['belongs_to_collection'].notnull() ).astype(int)
 test_orig['bool_belongs_to_collection'] =(test_orig['belongs_to_collection'].notnull() ).astype(int )
def truncated_normal(size, threshold=1):
values = truncnorm.rvs(-threshold, threshold, size=size)
 return torch.from_numpy(values ).float()<save_model>
train_orig['split'] = 'train'
test_orig['split'] = 'test'
torch.save(netG.state_dict() , 'netG.pth')
 torch.save(netD.state_dict() , 'netD.pth' )<import_modules>
train_test = pd.concat([train_orig[['popularity','budget','split','bool_belongs_to_collection']], test_orig[['popularity','budget','split','bool_belongs_to_collection']]] )
Conv2DTranspose, Conv2D, Flatten, Dropout, Embedding, ReLU
print(os.listdir(".. /input"))<set_options>
print(len(train_orig.columns))
print(len(test_orig.columns))
olang = train_orig.original_language.value_counts() [train_orig.original_language.value_counts() >5].index.tolist()
print(olang)
 print(len(olang))
train_orig_sample = train_orig[train_orig.original_language.isin(olang)].copy()
print(train_orig_sample.original_language.value_counts() )
tf.enable_eager_execution()<define_variables>
train_orig_sample.loc[:,'revenue'] = np.log(train_orig_sample['revenue'].fillna(0)+1 )
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' )<feature_engineering>
train_olang = pd.get_dummies(train_orig.original_language)[olang]
train_orig = pd.concat([train_orig,train_olang], axis=1)
dog_breed_dict = {}
for annotation in os.listdir(image_ann_dir):
annotations = annotation.split('-')
 dog_breed_dict[annotations[0]] = annotations[1]<load_pretrained>
def extract_id(cell):
return yaml.load(cell)[0]['id']
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<train_model>
test_olang = pd.get_dummies(test_orig.original_language)[olang]
test_orig = pd.concat([test_orig,test_olang], axis=1)
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))))<define_variables>
train_orig['cast_crew'] = train_orig.cast + ' ' + train_orig.crew
test_orig['cast_crew'] = test_orig.cast + ' ' + test_orig.crew
dog_images_np =(dog_images_np - 127.5)/ 127.5<data_type_conversions>
vec = TfidfVectorizer(analyzer='word',max_features=450,token_pattern=r"'name': ' (.*?)'")
 vec.fit(train_orig.cast_crew.fillna(''))
vocab = vec.get_feature_names()
vec = TfidfVectorizer(analyzer='word',vocabulary=vocab)
 train_crew_w = vec.fit_transform(train_orig.cast_crew.fillna(''))
test_crew_w = vec.transform(test_orig.cast_crew.fillna(''))
train_crew_w_cols = vec.get_feature_names()
train_crew_w_cols = ['crew_'+a for a in train_crew_w_cols]
print(train_crew_w.shape)
 print(test_crew_w.shape)
 print(train_crew_w_cols)
 train_crew_w = pd.DataFrame(train_crew_w.toarray() ,columns=train_crew_w_cols)
 test_crew_w = pd.DataFrame(test_crew_w.toarray() ,columns=train_crew_w_cols)
dog_features_tf = tf.cast(dog_images_np, 'float32' )<init_hyperparams>
vec = TfidfVectorizer(analyzer='word',max_features=100,token_pattern=r"'name': ' (.*?)'")
 vec.fit(train_orig.production_companies.fillna(''))
vocab = vec.get_feature_names()
vec = TfidfVectorizer(analyzer='word',vocabulary=vocab)
 train_production_companies_w = vec.fit_transform(train_orig.production_companies.fillna(''))
test_production_companies_w = vec.transform(test_orig.production_companies.fillna(''))
train_production_companies_w_cols = vec.get_feature_names()
train_production_companies_w_cols = ['prod_comp_'+a for a in train_production_companies_w_cols]
print(train_production_companies_w.shape)
 print(test_production_companies_w.shape)
 print(train_production_companies_w_cols)
 train_production_companies_w = pd.DataFrame(train_production_companies_w.toarray() ,columns=train_production_companies_w_cols)
 test_production_companies_w = pd.DataFrame(test_production_companies_w.toarray() ,columns=train_production_companies_w_cols)
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.3
scale_factor = 4 ** downsize_factor
lr_initial_d = 0.0002
lr_initial_g = 0.0002
lr_decay_steps = 1000
noise_dim = 128<create_dataframe>
vec = TfidfVectorizer(analyzer='word',max_features=20,token_pattern=r"'name': ' (.*?)'")
 vec.fit(train_orig.production_countries.fillna(''))
vocab = vec.get_feature_names()
vec = TfidfVectorizer(analyzer='word',vocabulary=vocab)
 train_production_countries_w = vec.fit_transform(train_orig.production_countries.fillna(''))
test_production_countries_w = vec.transform(test_orig.production_countries.fillna(''))
train_production_countries_w_cols = vec.get_feature_names()
train_production_countries_w_cols = ['prod_country_'+a for a in train_production_countries_w_cols]
print(train_production_countries_w.shape)
 print(test_production_countries_w.shape)
 print(train_production_countries_w_cols)
 train_production_countries_w = pd.DataFrame(train_production_countries_w.toarray() ,columns=train_production_countries_w_cols)
 test_production_countries_w = pd.DataFrame(test_production_countries_w.toarray() ,columns=train_production_countries_w_cols)
dog_features_data = tf.data.Dataset.from_tensor_slices(dog_features_tf ).shuffle(sample_size ).map(flip ).batch(batch_size )<choose_model_class>
vec = TfidfVectorizer(analyzer='word',max_features=50,token_pattern=r"'name': ' (.*?)'")
 train_belongs_to_collection_w = vec.fit_transform(train_orig.belongs_to_collection.fillna(''))
test_belongs_to_collection_w = vec.transform(test_orig.belongs_to_collection.fillna(''))
train_belongs_to_collection_w_cols = vec.get_feature_names()
train_belongs_to_collection_w_cols = ['collection_'+a for a in train_belongs_to_collection_w_cols]
print(train_belongs_to_collection_w.shape)
 print(test_belongs_to_collection_w.shape)
 print(train_belongs_to_collection_w_cols)
 train_belongs_to_collection_w = pd.DataFrame(train_belongs_to_collection_w.toarray() ,columns=train_belongs_to_collection_w_cols)
 test_belongs_to_collection_w = pd.DataFrame(test_belongs_to_collection_w.toarray() ,columns=train_belongs_to_collection_w_cols)
weight_initializer = tf.keras.initializers.TruncatedNormal(stddev=weight_init_std, mean=weight_init_mean,
seed=42 )<load_pretrained>
vec = TfidfVectorizer(stop_words='english',analyzer='word',max_features=50,token_pattern=r'(?u)\b[A-Za-z]{3,}\b')
 train_genres_w = vec.fit_transform(train_orig.genres.fillna(''))
test_genres_w = vec.transform(test_orig.genres.fillna(''))
train_genres_w_cols = vec.get_feature_names()
train_genres_w_cols = ['genre_'+a for a in train_genres_w_cols]
print(train_genres_w.shape)
 print(test_genres_w.shape)
 print(train_genres_w_cols)
 train_genres_w = pd.DataFrame(train_genres_w.toarray() ,columns=train_genres_w_cols)
 test_genres_w = pd.DataFrame(test_genres_w.toarray() ,columns=train_genres_w_cols)
 print(train_genres_w.shape)
 print(test_genres_w.shape )
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<choose_model_class>
train_orig['Keywords_tagline_overview'] = train_orig.title + ' ' + train_orig.Keywords +' ' + train_orig.tagline + ' ' + train_orig.overview
test_orig['Keywords_tagline_overview'] = test_orig.title + ' ' + test_orig.Keywords + ' ' + test_orig.tagline + ' ' + test_orig.overview
vec = TfidfVectorizer(stop_words='english',analyzer='word',max_features=60,token_pattern=r'(?u)\b[A-Za-z]{3,}\b')
 train_tagline_keyword_w = vec.fit_transform(train_orig.Keywords_tagline_overview.fillna(''))
train_tagline_keyword_w_cols = vec.get_feature_names()
print(train_tagline_keyword_w.shape)
 test_tagline_w = vec.transform(test_orig.Keywords_tagline_overview.fillna(''))
print(test_tagline_w.shape)
 train_tagline_keyword_w_cols = ['kw_tg_ow_' + a for a in train_tagline_keyword_w_cols]
train_tagline_keyword_w = pd.DataFrame(train_tagline_keyword_w.toarray() ,columns=train_tagline_keyword_w_cols)
 test_tagline_keyword_w = pd.DataFrame(test_tagline_w.toarray() ,columns=train_tagline_keyword_w_cols)
 train = pd.concat([train_orig,train_tagline_keyword_w,train_genres_w,train_belongs_to_collection_w,
train_production_companies_w,train_crew_w,train_production_countries_w], axis=1)
 test = pd.concat([test_orig,test_tagline_keyword_w,test_genres_w,test_belongs_to_collection_w,
test_production_companies_w,test_crew_w,test_production_countries_w], axis=1)
 print(train.shape)
 print(test.shape )
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_model>
train['bool_belongs_to_collection'] =(train['belongs_to_collection'].notnull() ).astype(int)
 test['bool_belongs_to_collection'] =(test['belongs_to_collection'].notnull() ).astype(int )
print(image_height // scale_factor, image_width // scale_factor, 512 )<choose_model_class>
len(train_tagline_keyword_w_cols )
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 = transposed_conv(model, 256, ksize=5, stride_size=2)
 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<define_variables>
train['release_date'] = pd.to_datetime(train['release_date'])
 test['release_date'] = pd.to_datetime(test['release_date'] )
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<choose_model_class>
train['release_month'] = train['release_date'].dt.month
test['release_month'] = test['release_date'].dt.month
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(Dropout(dropout_rate))
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<choose_model_class>
train['release_year'] = train['release_date'].dt.year
test['release_year'] = test['release_date'].dt.year
decision = dog_discriminator(generated_image)
 print(decision )<categorify>
train['release_dayofyear'] = train['release_date'].dt.dayofyear
test['release_dayofyear'] = test['release_date'].dt.dayofyear
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<categorify>
train['release_day_of_week'] = train['release_date'].dt.dayofweek
test['release_day_of_week'] = test['release_date'].dt.dayofweek
train['release_week'] = train['release_date'].dt.week
test['release_week'] = test['release_date'].dt.week
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<choose_model_class>
test['release_month'] = test['release_month'].fillna(9.0)
 test['release_year'] = test['release_year'].fillna(2014.0)
 test['release_week'] = test['release_week'].fillna(36.0 )
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 )<compute_train_metric>
train['bool_homepage'] =(train['homepage'].notnull() ).astype(int)
 test['bool_homepage'] =(test['homepage'].notnull() ).astype(int )
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<compute_train_metric>
train['production_companies_len'] = train['production_companies'].str.len()
test['production_companies_len'] = test['production_companies'].str.len()
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 
<train_model>
train['production_countries_len'] = train['production_countries'].str.len()
test['production_countries_len'] = test['production_countries'].str.len()
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 )<define_variables>
train['Keywords_len'] = train['Keywords'].str.len()
test['Keywords_len'] = test['Keywords'].str.len()
EPOCHS = 320
num_examples_to_generate = 16
seed = tf.random.normal([num_examples_to_generate, noise_dim] )<compute_train_metric>
train['title_len'] = train['title'].str.len()
test['title_len'] = test['title'].str.len()
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))<init_hyperparams>
train['genres_len'] = train['genres'].str.len()
test['genres_len'] = test['genres'].str.len()
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 )<load_pretrained>
train['cast_crew_len'] = train['cast'].str.len() + train['crew'].str.len()
test['cast_crew_len'] = test['cast'].str.len() + test['crew'].str.len()
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.' )<train_model>
train.loc[(train['release_year']>2019),'release_year']=train['release_year'].median()
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)% 50 == 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.' )<train_model>
train['month_into_year'] = train['release_month']*train['release_year']
train(dog_features_data, EPOCHS )<load_pretrained>
test.loc[(test['release_year']>2019),'release_year']=test['release_year'].median()
zip_images()<define_variables>
vcast_crew_len = test['cast_crew_len'].median()
test['cast_crew_len'].fillna(vcast_crew_len, inplace=True)
 test['runtime'].fillna(test['runtime'].median() ,inplace=True)
 test['release_month'].fillna(test['release_month'].median() ,inplace=True)
 test['title_len'].fillna(test['title_len'].median() ,inplace=True)
 test['release_year'].fillna(test['release_year'].median() ,inplace=True)
 test['release_day_of_week'].fillna(test['release_day_of_week'].median() ,inplace=True)
 test['release_dayofyear'].fillna(test['release_dayofyear'].median() ,inplace=True)
 test['genres_len'].fillna(test['genres_len'].median() ,inplace=True)
 test['production_companies_len'].fillna(test['production_companies_len'].median() ,inplace=True)
 test['production_countries_len'].fillna(test['production_countries_len'].median() ,inplace=True)
 test['Keywords_len'].fillna(test['Keywords_len'].median() ,inplace=True )
ComputeLB = False
DogsOnly = False
ROOT = '.. /input/generative-dog-images/'
if not ComputeLB: ROOT = '.. /input/'
IMAGES = os.listdir(ROOT + 'all-dogs/all-dogs/')
 breeds = os.listdir(ROOT + 'annotation/Annotation/')
 idxIn = 0; namesIn = []
imagesIn = np.zeros(( 25000,64,64,3))
if DogsOnly:
for breed in breeds:
for dog in os.listdir(ROOT+'annotation/Annotation/'+breed):
try: img = Image.open(ROOT+'all-dogs/all-dogs/'+dog+'.jpg')
 except: continue
tree = ET.parse(ROOT+'annotation/Annotation/'+breed+'/'+dog)
 root = tree.getroot()
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)
 w = np.min(( xmax - xmin, ymax - ymin))
img2 = img.crop(( xmin, ymin, xmin+w, ymin+w))
img2 = img2.resize(( 64,64), Image.ANTIALIAS)
 imagesIn[idxIn,:,:,:] = np.asarray(img2)
 namesIn.append(breed)
 idxIn += 1
else:
x = np.random.choice(np.arange(20000),10000)
 for k in range(len(x)) :
img = Image.open(ROOT + 'all-dogs/all-dogs/' + IMAGES[x[k]])
 w = img.size[0]; h = img.size[1];
if(k%2==0)|(k%3==0):
w2 = 100; h2 = int(h/(w/100))
a = 18; b = 0
else:
a=0; b=0
if w<h:
w2 = 64; h2 = int(( 64/w)*h)
 b =(h2-64)//2
else:
h2 = 64; w2 = int(( 64/h)*w)
 a =(w2-64)//2
img = img.resize(( w2,h2), Image.ANTIALIAS)
 img = img.crop(( 0+a, 0+b, 64+a, 64+b))
imagesIn[idxIn,:,:,:] = np.asarray(img)
 namesIn.append(IMAGES[x[k]])
 idxIn += 1
x = np.random.randint(0,idxIn,25)
 for k in range(5):
plt.figure(figsize=(15,3))
for j in range(5):
plt.subplot(1,5,j+1)
 img = Image.fromarray(imagesIn[x[k*5+j],:,:,:].astype('uint8'))
plt.axis('off')
 if not DogsOnly: plt.title(namesIn[x[k*5+j]],fontsize=11)
 else: plt.title(namesIn[x[k*5+j]].split('-')[1],fontsize=11)
 plt.imshow(img)
 plt.show()<import_modules>
test['month_into_year'] = test['release_month']*test['release_year']
from keras.models import Model
from keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D, Reshape, BatchNormalization
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import LearningRateScheduler
from keras.optimizers import SGD, Adam<categorify>
features = ['bool_homepage', 'release_dayofyear','production_companies_len', 'production_countries_len', 'Keywords_len' , 'cast_crew_len','budget','popularity','runtime','release_month','release_day_of_week','release_week','genres_len','bool_belongs_to_collection', 'title_len','release_year']
direct_input = Input(( 10000,))
x = Dense(2048, activation='elu' )(direct_input)
 x = Reshape(( 8,8,32))(x)
 x = Conv2D(128,(3, 3), activation='elu', padding='same' )(x)
 x = UpSampling2D(( 2, 2))(x)
 x = Conv2D(64,(3, 3), activation='elu', padding='same' )(x)
 x = UpSampling2D(( 2, 2))(x)
 x = Conv2D(32,(3, 3), activation='elu', padding='same' )(x)
 x = UpSampling2D(( 2, 2))(x)
 decoded = Conv2D(3,(3, 3), activation='sigmoid', padding='same' )(x)
 decoder = Model(direct_input, decoded)
 decoder.compile(optimizer=Adam(lr=0.003), loss='binary_crossentropy')
 decoder.summary()<prepare_x_and_y>
train['log_revenue'] = np.log(train['revenue'].fillna(0)+1 )
idx = np.random.randint(0,idxIn,10000)
 train_y = imagesIn[idx,:,:,:]/255.
 train_X = np.zeros(( 10000,10000))
for i in range(10000): train_X[i,i] = 1<categorify>
features = features+olang+train_tagline_keyword_w_cols+train_genres_w_cols+train_belongs_to_collection_w_cols \
+train_production_companies_w_cols + train_crew_w_cols+ train_production_countries_w_cols
lr = 0.005
for k in range(50):
annealer = LearningRateScheduler(lambda x: lr)
 h = decoder.fit(train_X, train_y, epochs = 20, batch_size=256, callbacks=[annealer], verbose=0)
 if k%5==4: print('Epoch',(k+1)*10,'/500 - loss =',h.history['loss'][-1])
 if h.history['loss'][-1]<0.54: lr = 0.001<drop_column>
target_column = 'revenue'
columns_for_prediction=features
X = train[columns_for_prediction].copy()
y_scale = preprocessing.MinMaxScaler()
y = y_scale.fit_transform(train[target_column].values.reshape(-1, 1))
X_unseen = test[columns_for_prediction].copy()
scale = preprocessing.StandardScaler()
X = pd.DataFrame(scale.fit_transform(X),columns=columns_for_prediction)
 X_unseen = pd.DataFrame(scale.transform(test[columns_for_prediction]),columns=columns_for_prediction)
 budget_min = X['budget'].quantile(0.28)
 X['budget'] = X['budget'].replace(0,budget_min)
 X_unseen['budget'] = X_unseen['budget'].replace(0,budget_min)
del train_X, train_y, imagesIn<categorify>
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.1, random_state=2019)
 params = {'n_estimators': 700, 'max_depth': 4, 'min_samples_split': 2,
'learning_rate': 0.01, 'loss': 'ls'}
reg = GradientBoostingRegressor(**params ).fit(X_train, y_train)
 score = reg.score(X_test, y_test)
 print('Test score %d'%score)
 preds = reg.predict(X_test)
 err = mean_squared_error(y_test, preds)
 print('Test mse %d'%err)
 reg = GradientBoostingRegressor(n_estimators=700 ).fit(X, y)
 score = reg.score(X, y)
 print('Train score %d'%score)
 preds_first = reg.predict(X_unseen )
print('Generate Random Dogs')
 for k in range(5):
plt.figure(figsize=(15,3))
for j in range(5):
xx = np.zeros(( 10000))
xx[np.random.randint(10000)] = 1
xx[np.random.randint(10000)] = 0.75
xx = xx/(np.sqrt(xx.dot(xx.T)))
 plt.subplot(1,5,j+1)
 img = decoder.predict(xx.reshape(( -1,10000)))
 img = Image.fromarray(( 255*img ).astype('uint8' ).reshape(( 64,64,3)))
 plt.axis('off')
 plt.imshow(img)
 plt.show()<categorify>
print('Recall from Memory Dogs')
 for k in range(5):
plt.figure(figsize=(15,3))
for j in range(5):
xx = np.zeros(( 10000))
xx[np.random.randint(10000)] = 1
plt.subplot(1,5,j+1)
 img = decoder.predict(xx.reshape(( -1,10000)))
 img = Image.fromarray(( 255*img ).astype('uint8' ).reshape(( 64,64,3)))
 plt.axis('off')
 plt.imshow(img)
 plt.show()<predict_on_test>
for k in range(6):
if(k==0)|(k==2)|(k==4):
print('Walk in Latent Space')
 a = np.random.randint(10000)
 b = np.random.randint(10000)
 plt.figure(figsize=(15,3))
for j in range(5):
xx = np.zeros(( 10000))
theta =(( k%2)*5+j)/9
xx[a] = theta; xx[b] = 1-theta
xx = xx/(np.sqrt(xx.dot(xx.T)))
 plt.subplot(1,5,j+1)
 img = decoder.predict(xx.reshape(( -1,10000)))
 img = Image.fromarray(( 255*img ).astype('uint8' ).reshape(( 64,64,3)))
 plt.axis('off')
 plt.imshow(img)
 plt.show()<categorify>
median_revenue = train[target_column].median()
preds = preds_estop
preds = y_scale.inverse_transform(preds.reshape(-1, 1))
preds[preds < 0] = median_revenue
subm['revenue'] = preds
subm.to_csv('submission.csv', index=False)
 print(subm.head() )
<choose_model_class><EOS> 
test['revenue'] = subm['revenue']