Kaggle competition: Generative Dog Images
<SOS> metric: PostProcessorKernel Kaggle data source: generative-dog-images<filter>
%matplotlib inline
warnings.filterwarnings('ignore', category=FutureWarning)
 device = 'cuda' if torch.cuda.is_available() else 'cpu'
index_ocur = train[train['time_to_failure'].diff(-1)<0].index
print(index_ocur )<data_type_conversions>
config = {'DataLoader': {'batch_size': 128,
'shuffle': True},
'Generator': {'latent_dim': 120,
'embed_dim': 32,
'ch': 64,
'num_classes': 120,
'use_attn': True},
'Discriminator': {'ch': 64,
'num_classes': 120,
'use_attn': True},
'sample_latents': {'latent_dim': 120,
'num_classes': 120},
'num_iterations': 26000,
'decay_start_iteration': 25000,
'd_steps': 1,
'lr_G': 2e-4,
'lr_D': 4e-4,
'betas':(0.0, 0.999),
'margin': 1.0,
'gamma': 0.1,
'ema': 0.999,
'seed': 42}
index_ocur_array = np.array(index_ocur.tolist())
 index_ocur_array<set_options>
def seed_everything(seed):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
seed_everything(config['seed'] )
del index_ocur
gc.collect()<define_variables>
root_images = '.. /input/all-dogs/all-dogs/'
root_annots = '.. /input/annotation/Annotation/'
intervals = [index_ocur_array[0]]+[index_ocur_array[i]-index_ocur_array[i-1] for i in range(1,len(index_ocur_array)) ]\
+[train.shape[0]-index_ocur_array[-1]]
intervals<define_variables>
all_files = os.listdir(root_images )
index_beg = [0] + list(np.array(index_ocur_array)+1)
 blocks = [(index_beg[i], int(intervals[i]/150000)) for i in range(len(intervals)) ]
blocks<train_on_grid>
breeds = glob.glob(root_annots+'*')
 annotations = []
for breed in breeds:
annotations += glob.glob(breed+'/*' )
rows = 150_000
segments = int(np.floor(train.shape[0] / rows))
def add_trend_feature(arr, abs_values=False):
idx = np.array(range(len(arr)))
 if abs_values:
arr = np.abs(arr)
 lr = LinearRegression()
lr.fit(idx.reshape(-1, 1), arr)
 return lr.coef_[0]
def classic_sta_lta(x, length_sta, length_lta):
sta = np.cumsum(x ** 2)
 sta = np.require(sta, dtype=np.float)
 lta = sta.copy()
sta[length_sta:] = sta[length_sta:] - sta[:-length_sta]
sta /= length_sta
lta[length_lta:] = lta[length_lta:] - lta[:-length_lta]
lta /= length_lta
sta[:length_lta - 1] = 0
dtiny = np.finfo(0.0 ).tiny
idx = lta < dtiny
lta[idx] = dtiny
return sta / lta
X_tr = pd.DataFrame(index=range(segments), dtype=np.float64)
 y_tr = pd.DataFrame(index=range(segments), dtype=np.float64, columns=['time_to_failure'])
 total_mean = train['acoustic_data'].mean()
total_std = train['acoustic_data'].std()
total_max = train['acoustic_data'].max()
total_min = train['acoustic_data'].min()
total_sum = train['acoustic_data'].sum()
total_abs_sum = np.abs(train['acoustic_data'] ).sum()
def calc_change_rate(x):
change =(np.diff(x)/ x[:-1] ).values
change = change[np.nonzero(change)[0]]
change = change[~np.isnan(change)]
change = change[change != -np.inf]
change = change[change != np.inf]
return np.mean(change)
 segment = 0
for(index_start, blocknum)in tqdm_notebook(blocks):
for block in tqdm_notebook(range(blocknum)) :
seg = train.iloc[index_start+block*rows:index_start+block*rows+rows]
x = pd.Series(seg['acoustic_data'].values)
 y = seg['time_to_failure'].values[-1]
data_fft = abs(fft(x)) [range(1,int(len(x)/2)) ]/len(x)
 y_tr.loc[segment, 'time_to_failure'] = y
X_tr.loc[segment, 'mean'] = x.mean()
X_tr.loc[segment, 'std'] = x.std()
X_tr.loc[segment, 'max'] = x.max()
X_tr.loc[segment, 'min'] = x.min()
X_tr.loc[segment, 'fft_mean'] = data_fft.mean()
X_tr.loc[segment, 'fft_std'] = data_fft.std()
X_tr.loc[segment, 'fft_max'] = data_fft.max()
X_tr.loc[segment, 'fft_min'] = data_fft.min()
X_tr.loc[segment, 'mean_change_abs'] = np.mean(np.diff(x))
X_tr.loc[segment, 'mean_change_rate'] = calc_change_rate(x)
 X_tr.loc[segment, 'abs_max'] = np.abs(x ).max()
X_tr.loc[segment, 'abs_min'] = np.abs(x ).min()
X_tr.loc[segment, 'std_first_50000'] = x[:50000].std()
X_tr.loc[segment, 'std_last_50000'] = x[-50000:].std()
X_tr.loc[segment, 'std_first_10000'] = x[:10000].std()
X_tr.loc[segment, 'std_last_10000'] = x[-10000:].std()
X_tr.loc[segment, 'avg_first_50000'] = x[:50000].mean()
X_tr.loc[segment, 'avg_last_50000'] = x[-50000:].mean()
X_tr.loc[segment, 'avg_first_10000'] = x[:10000].mean()
X_tr.loc[segment, 'avg_last_10000'] = x[-10000:].mean()
X_tr.loc[segment, 'min_first_50000'] = x[:50000].min()
X_tr.loc[segment, 'min_last_50000'] = x[-50000:].min()
X_tr.loc[segment, 'min_first_10000'] = x[:10000].min()
X_tr.loc[segment, 'min_last_10000'] = x[-10000:].min()
X_tr.loc[segment, 'max_first_50000'] = x[:50000].max()
X_tr.loc[segment, 'max_last_50000'] = x[-50000:].max()
X_tr.loc[segment, 'max_first_10000'] = x[:10000].max()
X_tr.loc[segment, 'max_last_10000'] = x[-10000:].max()
X_tr.loc[segment, 'fft_std_first_50000'] = data_fft[:50000].std()
X_tr.loc[segment, 'fft_std_last_50000'] = data_fft[-50000:].std()
X_tr.loc[segment, 'fft_std_first_10000'] = data_fft[:10000].std()
X_tr.loc[segment, 'fft_std_last_10000'] = data_fft[-10000:].std()
X_tr.loc[segment, 'fft_avg_first_50000'] = data_fft[:50000].mean()
X_tr.loc[segment, 'fft_avg_last_50000'] = data_fft[-50000:].mean()
X_tr.loc[segment, 'fft_avg_first_10000'] = data_fft[:10000].mean()
X_tr.loc[segment, 'fft_avg_last_10000'] = data_fft[-10000:].mean()
X_tr.loc[segment, 'fft_min_first_50000'] = data_fft[:50000].min()
X_tr.loc[segment, 'fft_min_last_50000'] = data_fft[-50000:].min()
X_tr.loc[segment, 'fft_min_first_10000'] = data_fft[:10000].min()
X_tr.loc[segment, 'fft_min_last_10000'] = data_fft[-10000:].min()
X_tr.loc[segment, 'fft_max_first_50000'] = data_fft[:50000].max()
X_tr.loc[segment, 'fft_max_last_50000'] = data_fft[-50000:].max()
X_tr.loc[segment, 'fft_max_first_10000'] = data_fft[:10000].max()
X_tr.loc[segment, 'fft_max_last_10000'] = data_fft[-10000:].max()
X_tr.loc[segment, 'max_to_min'] = x.max() / np.abs(x.min())
 X_tr.loc[segment, 'max_to_min_diff'] = x.max() - np.abs(x.min())
 X_tr.loc[segment, 'count_big'] = len(x[np.abs(x)> 500])
 X_tr.loc[segment, 'sum'] = x.sum()
X_tr.loc[segment, 'mean_change_rate_first_50000'] = calc_change_rate(x[:50000])
 X_tr.loc[segment, 'mean_change_rate_last_50000'] = calc_change_rate(x[-50000:])
 X_tr.loc[segment, 'mean_change_rate_first_10000'] = calc_change_rate(x[:10000])
 X_tr.loc[segment, 'mean_change_rate_last_10000'] = calc_change_rate(x[-10000:])
 X_tr.loc[segment, 'q95'] = np.quantile(x, 0.95)
 X_tr.loc[segment, 'q99'] = np.quantile(x, 0.99)
 X_tr.loc[segment, 'q05'] = np.quantile(x, 0.05)
 X_tr.loc[segment, 'q01'] = np.quantile(x, 0.01)
 X_tr.loc[segment, 'fft_q95'] = np.quantile(data_fft, 0.95)
 X_tr.loc[segment, 'fft_q99'] = np.quantile(data_fft, 0.99)
 X_tr.loc[segment, 'fft_q05'] = np.quantile(data_fft, 0.05)
 X_tr.loc[segment, 'fft_q01'] = np.quantile(data_fft, 0.01)
 X_tr.loc[segment, 'abs_q95'] = np.quantile(np.abs(x), 0.95)
 X_tr.loc[segment, 'abs_q99'] = np.quantile(np.abs(x), 0.99)
 X_tr.loc[segment, 'abs_q05'] = np.quantile(np.abs(x), 0.05)
 X_tr.loc[segment, 'abs_q01'] = np.quantile(np.abs(x), 0.01)
 X_tr.loc[segment, 'trend'] = add_trend_feature(x)
 X_tr.loc[segment, 'abs_trend'] = add_trend_feature(x, abs_values=True)
 X_tr.loc[segment, 'abs_mean'] = np.abs(x ).mean()
X_tr.loc[segment, 'abs_std'] = np.abs(x ).std()
X_tr.loc[segment, 'mad'] = x.mad()
X_tr.loc[segment, 'kurt'] = x.kurtosis()
X_tr.loc[segment, 'skew'] = x.skew()
X_tr.loc[segment, 'med'] = x.median()
X_tr.loc[segment, 'Hilbert_mean'] = np.abs(hilbert(x)).mean()
X_tr.loc[segment, 'Hann_window_mean'] =(convolve(x, hann(150), mode='same')/ sum(hann(150)) ).mean()
X_tr.loc[segment, 'classic_sta_lta1_mean'] = classic_sta_lta(x, 500, 10000 ).mean()
X_tr.loc[segment, 'classic_sta_lta2_mean'] = classic_sta_lta(x, 5000, 100000 ).mean()
X_tr.loc[segment, 'classic_sta_lta3_mean'] = classic_sta_lta(x, 3333, 6666 ).mean()
X_tr.loc[segment, 'classic_sta_lta4_mean'] = classic_sta_lta(x, 10000, 25000 ).mean()
X_tr.loc[segment, 'classic_sta_lta5_mean'] = classic_sta_lta(x, 50, 1000 ).mean()
X_tr.loc[segment, 'classic_sta_lta6_mean'] = classic_sta_lta(x, 100, 5000 ).mean()
X_tr.loc[segment, 'classic_sta_lta7_mean'] = classic_sta_lta(x, 333, 666 ).mean()
X_tr.loc[segment, 'classic_sta_lta8_mean'] = classic_sta_lta(x, 4000, 10000 ).mean()
X_tr.loc[segment, 'Moving_average_700_mean'] = x.rolling(window=700 ).mean().mean(skipna=True)
 ewma = pd.Series.ewm
X_tr.loc[segment, 'exp_Moving_average_300_mean'] =(ewma(x, span=300 ).mean() ).mean(skipna=True)
 X_tr.loc[segment, 'exp_Moving_average_3000_mean'] = ewma(x, span=3000 ).mean().mean(skipna=True)
 X_tr.loc[segment, 'exp_Moving_average_30000_mean'] = ewma(x, span=30000 ).mean().mean(skipna=True)
 no_of_std = 3
X_tr.loc[segment, 'MA_700MA_std_mean'] = x.rolling(window=700 ).std().mean()
X_tr.loc[segment,'MA_700MA_BB_high_mean'] =(X_tr.loc[segment, 'Moving_average_700_mean'] + no_of_std * X_tr.loc[segment, 'MA_700MA_std_mean'] ).mean()
X_tr.loc[segment,'MA_700MA_BB_low_mean'] =(X_tr.loc[segment, 'Moving_average_700_mean'] - no_of_std * X_tr.loc[segment, 'MA_700MA_std_mean'] ).mean()
X_tr.loc[segment, 'MA_400MA_std_mean'] = x.rolling(window=400 ).std().mean()
X_tr.loc[segment,'MA_400MA_BB_high_mean'] =(X_tr.loc[segment, 'Moving_average_700_mean'] + no_of_std * X_tr.loc[segment, 'MA_400MA_std_mean'] ).mean()
X_tr.loc[segment,'MA_400MA_BB_low_mean'] =(X_tr.loc[segment, 'Moving_average_700_mean'] - no_of_std * X_tr.loc[segment, 'MA_400MA_std_mean'] ).mean()
X_tr.loc[segment, 'MA_1000MA_std_mean'] = x.rolling(window=1000 ).std().mean()
X_tr.drop('Moving_average_700_mean', axis=1, inplace=True)
 X_tr.loc[segment, 'iqr'] = np.subtract(*np.percentile(x, [75, 25]))
X_tr.loc[segment, 'q999'] = np.quantile(x,0.999)
 X_tr.loc[segment, 'q001'] = np.quantile(x,0.001)
 X_tr.loc[segment, 'ave10'] = stats.trim_mean(x, 0.1)
 for windows in [10, 100, 1000]:
x_roll_std = x.rolling(windows ).std().dropna().values
x_roll_mean = x.rolling(windows ).mean().dropna().values
X_tr.loc[segment, 'ave_roll_std_' + str(windows)] = x_roll_std.mean()
X_tr.loc[segment, 'std_roll_std_' + str(windows)] = x_roll_std.std()
X_tr.loc[segment, 'max_roll_std_' + str(windows)] = x_roll_std.max()
X_tr.loc[segment, 'min_roll_std_' + str(windows)] = x_roll_std.min()
X_tr.loc[segment, 'q01_roll_std_' + str(windows)] = np.quantile(x_roll_std, 0.01)
 X_tr.loc[segment, 'q05_roll_std_' + str(windows)] = np.quantile(x_roll_std, 0.05)
 X_tr.loc[segment, 'q95_roll_std_' + str(windows)] = np.quantile(x_roll_std, 0.95)
 X_tr.loc[segment, 'q99_roll_std_' + str(windows)] = np.quantile(x_roll_std, 0.99)
 X_tr.loc[segment, 'av_change_abs_roll_std_' + str(windows)] = np.mean(np.diff(x_roll_std))
X_tr.loc[segment, 'av_change_rate_roll_std_' + str(windows)] = np.mean(np.nonzero(( np.diff(x_roll_std)/ x_roll_std[:-1])) [0])
 X_tr.loc[segment, 'abs_max_roll_std_' + str(windows)] = np.abs(x_roll_std ).max()
X_tr.loc[segment, 'ave_roll_mean_' + str(windows)] = x_roll_mean.mean()
X_tr.loc[segment, 'std_roll_mean_' + str(windows)] = x_roll_mean.std()
X_tr.loc[segment, 'max_roll_mean_' + str(windows)] = x_roll_mean.max()
X_tr.loc[segment, 'min_roll_mean_' + str(windows)] = x_roll_mean.min()
X_tr.loc[segment, 'q01_roll_mean_' + str(windows)] = np.quantile(x_roll_mean, 0.01)
 X_tr.loc[segment, 'q05_roll_mean_' + str(windows)] = np.quantile(x_roll_mean, 0.05)
 X_tr.loc[segment, 'q95_roll_mean_' + str(windows)] = np.quantile(x_roll_mean, 0.95)
 X_tr.loc[segment, 'q99_roll_mean_' + str(windows)] = np.quantile(x_roll_mean, 0.99)
 X_tr.loc[segment, 'av_change_abs_roll_mean_' + str(windows)] = np.mean(np.diff(x_roll_mean))
X_tr.loc[segment, 'av_change_rate_roll_mean_' + str(windows)] = np.mean(np.nonzero(( np.diff(x_roll_mean)/ x_roll_mean[:-1])) [0])
 X_tr.loc[segment, 'abs_max_roll_mean_' + str(windows)] = np.abs(x_roll_mean ).max()
segment+=1<count_missing_values>
breed_map = {}
for annotation in annotations:
breed = annotation.split('/')[-2]
index = breed.split('-')[0]
breed_map.setdefault(index, breed )
X_tr.isnull().sum().sum()<split>
all_labels = [breed_map[file.split('_')[0]] for file in all_files]
le = LabelEncoder()
all_labels = le.fit_transform(all_labels )
X_tr = X_tr[:-10]
y_tr = y_tr[:-10]<compute_test_metric>
all_bboxes = [load_bbox(file)for file in all_files]
np.abs(X_tr.corrwith(y_tr['time_to_failure'])).sort_values(ascending=False ).head(12 )<feature_engineering>
all_images = [load_bboxcrop_resized_image(f, b)for f, b in zip(all_files, resized_bboxes)]
all_images = np.array(all_images )
means_dict = {}
for col in X_tr.columns:
if X_tr[col].isnull().any() :
print(col)
 mean_value = X_tr.loc[X_tr[col] != -np.inf, col].mean()
X_tr.loc[X_tr[col] == -np.inf, col] = mean_value
X_tr[col] = X_tr[col].fillna(mean_value)
 means_dict[col] = mean_value<train_model>
class DogDataset(Dataset):
def __init__(self, images, labels):
super().__init__()
self.images = images
self.labels = labels
self.transform = A.Compose([A.HorizontalFlip(p=0.5), ToTensor() ])
 def __len__(self):
return len(self.images)
 def __getitem__(self, idx):
img, label = self.images[idx], self.labels[idx]
img = self.transform(image=img)['image']
label = torch.as_tensor(label, dtype=torch.long)
 return img, label
scaler = StandardScaler()
scaler.fit(X_tr)
 X_train_scaled = pd.DataFrame(scaler.transform(X_tr), columns=X_tr.columns )<load_from_csv>
def get_dataiterator(images, labels, dataloader_params, device='cpu'):
train_dataset = DogDataset(images, labels)
 train_dataloader = DataLoader(train_dataset, **dataloader_params)
 batch_size = dataloader_params['batch_size']
while True:
for imgs, labels in train_dataloader:
if batch_size != imgs.size(0):
break
else:
imgs, labels = imgs.to(device), labels.to(device)
 imgs +=(1.0 / 128.0)* torch.rand_like(imgs)
 yield imgs, labels
submission = pd.read_csv('.. /input/sample_submission.csv', index_col='seg_id')
 X_test = pd.DataFrame(columns=X_tr.columns, dtype=np.float64, index=submission.index)
 plt.figure(figsize=(22, 16))
for i, seg_id in enumerate(tqdm_notebook(X_test.index)) :
seg = pd.read_csv('.. /input/test/' + seg_id + '.csv')
 x = pd.Series(seg['acoustic_data'].values)
 data_fft = abs(fft(x)) [range(1,int(len(x)/2)) ]/len(x)
 X_test.loc[seg_id, 'mean'] = x.mean()
X_test.loc[seg_id, 'std'] = x.std()
X_test.loc[seg_id, 'max'] = x.max()
X_test.loc[seg_id, 'min'] = x.min()
X_test.loc[seg_id, 'fft_mean'] = data_fft.mean()
X_test.loc[seg_id, 'fft_std'] = data_fft.std()
X_test.loc[seg_id, 'fft_max'] = data_fft.max()
X_test.loc[seg_id, 'fft_min'] = data_fft.min()
X_test.loc[seg_id, 'mean_change_abs'] = np.mean(np.diff(x))
X_test.loc[seg_id, 'mean_change_rate'] = calc_change_rate(x)
 X_test.loc[seg_id, 'abs_max'] = np.abs(x ).max()
X_test.loc[seg_id, 'abs_min'] = np.abs(x ).min()
X_test.loc[seg_id, 'std_first_50000'] = x[:50000].std()
X_test.loc[seg_id, 'std_last_50000'] = x[-50000:].std()
X_test.loc[seg_id, 'std_first_10000'] = x[:10000].std()
X_test.loc[seg_id, 'std_last_10000'] = x[-10000:].std()
X_test.loc[seg_id, 'avg_first_50000'] = x[:50000].mean()
X_test.loc[seg_id, 'avg_last_50000'] = x[-50000:].mean()
X_test.loc[seg_id, 'avg_first_10000'] = x[:10000].mean()
X_test.loc[seg_id, 'avg_last_10000'] = x[-10000:].mean()
X_test.loc[seg_id, 'min_first_50000'] = x[:50000].min()
X_test.loc[seg_id, 'min_last_50000'] = x[-50000:].min()
X_test.loc[seg_id, 'min_first_10000'] = x[:10000].min()
X_test.loc[seg_id, 'min_last_10000'] = x[-10000:].min()
X_test.loc[seg_id, 'max_first_50000'] = x[:50000].max()
X_test.loc[seg_id, 'max_last_50000'] = x[-50000:].max()
X_test.loc[seg_id, 'max_first_10000'] = x[:10000].max()
X_test.loc[seg_id, 'max_last_10000'] = x[-10000:].max()
X_test.loc[seg_id, 'fft_std_first_50000'] = data_fft[:50000].std()
X_test.loc[seg_id, 'fft_std_last_50000'] = data_fft[-50000:].std()
X_test.loc[seg_id, 'fft_std_first_10000'] = data_fft[:10000].std()
X_test.loc[seg_id, 'fft_std_last_10000'] = data_fft[-10000:].std()
X_test.loc[seg_id, 'fft_avg_first_50000'] = data_fft[:50000].mean()
X_test.loc[seg_id, 'fft_avg_last_50000'] = data_fft[-50000:].mean()
X_test.loc[seg_id, 'fft_avg_first_10000'] = data_fft[:10000].mean()
X_test.loc[seg_id, 'fft_avg_last_10000'] = data_fft[-10000:].mean()
X_test.loc[seg_id, 'fft_min_first_50000'] = data_fft[:50000].min()
X_test.loc[seg_id, 'fft_min_last_50000'] = data_fft[-50000:].min()
X_test.loc[seg_id, 'fft_min_first_10000'] = data_fft[:10000].min()
X_test.loc[seg_id, 'fft_min_last_10000'] = data_fft[-10000:].min()
X_test.loc[seg_id, 'fft_max_first_50000'] = data_fft[:50000].max()
X_test.loc[seg_id, 'fft_max_last_50000'] = data_fft[-50000:].max()
X_test.loc[seg_id, 'fft_max_first_10000'] = data_fft[:10000].max()
X_test.loc[seg_id, 'fft_max_last_10000'] = data_fft[-10000:].max()
X_test.loc[seg_id, 'max_to_min'] = x.max() / np.abs(x.min())
 X_test.loc[seg_id, 'max_to_min_diff'] = x.max() - np.abs(x.min())
 X_test.loc[seg_id, 'count_big'] = len(x[np.abs(x)> 500])
 X_test.loc[seg_id, 'sum'] = x.sum()
X_test.loc[seg_id, 'mean_change_rate_first_50000'] = calc_change_rate(x[:50000])
 X_test.loc[seg_id, 'mean_change_rate_last_50000'] = calc_change_rate(x[-50000:])
 X_test.loc[seg_id, 'mean_change_rate_first_10000'] = calc_change_rate(x[:10000])
 X_test.loc[seg_id, 'mean_change_rate_last_10000'] = calc_change_rate(x[-10000:])
 X_test.loc[seg_id, 'q95'] = np.quantile(x,0.95)
 X_test.loc[seg_id, 'q99'] = np.quantile(x,0.99)
 X_test.loc[seg_id, 'q05'] = np.quantile(x,0.05)
 X_test.loc[seg_id, 'q01'] = np.quantile(x,0.01)
 X_test.loc[seg_id, 'fft_q95'] = np.quantile(data_fft, 0.95)
 X_test.loc[seg_id, 'fft_q99'] = np.quantile(data_fft, 0.99)
 X_test.loc[seg_id, 'fft_q05'] = np.quantile(data_fft, 0.05)
 X_test.loc[seg_id, 'fft_q01'] = np.quantile(data_fft, 0.01)
 X_test.loc[seg_id, 'abs_q95'] = np.quantile(np.abs(x), 0.95)
 X_test.loc[seg_id, 'abs_q99'] = np.quantile(np.abs(x), 0.99)
 X_test.loc[seg_id, 'abs_q05'] = np.quantile(np.abs(x), 0.05)
 X_test.loc[seg_id, 'abs_q01'] = np.quantile(np.abs(x), 0.01)
 X_test.loc[seg_id, 'trend'] = add_trend_feature(x)
 X_test.loc[seg_id, 'abs_trend'] = add_trend_feature(x, abs_values=True)
 X_test.loc[seg_id, 'abs_mean'] = np.abs(x ).mean()
X_test.loc[seg_id, 'abs_std'] = np.abs(x ).std()
X_test.loc[seg_id, 'mad'] = x.mad()
X_test.loc[seg_id, 'kurt'] = x.kurtosis()
X_test.loc[seg_id, 'skew'] = x.skew()
X_test.loc[seg_id, 'med'] = x.median()
X_test.loc[seg_id, 'Hilbert_mean'] = np.abs(hilbert(x)).mean()
X_test.loc[seg_id, 'Hann_window_mean'] =(convolve(x, hann(150), mode='same')/ sum(hann(150)) ).mean()
X_test.loc[seg_id, 'classic_sta_lta1_mean'] = classic_sta_lta(x, 500, 10000 ).mean()
X_test.loc[seg_id, 'classic_sta_lta2_mean'] = classic_sta_lta(x, 5000, 100000 ).mean()
X_test.loc[seg_id, 'classic_sta_lta3_mean'] = classic_sta_lta(x, 3333, 6666 ).mean()
X_test.loc[seg_id, 'classic_sta_lta4_mean'] = classic_sta_lta(x, 10000, 25000 ).mean()
X_test.loc[seg_id, 'classic_sta_lta5_mean'] = classic_sta_lta(x, 50, 1000 ).mean()
X_test.loc[seg_id, 'classic_sta_lta6_mean'] = classic_sta_lta(x, 100, 5000 ).mean()
X_test.loc[seg_id, 'classic_sta_lta7_mean'] = classic_sta_lta(x, 333, 666 ).mean()
X_test.loc[seg_id, 'classic_sta_lta8_mean'] = classic_sta_lta(x, 4000, 10000 ).mean()
X_test.loc[seg_id, 'Moving_average_700_mean'] = x.rolling(window=700 ).mean().mean(skipna=True)
 ewma = pd.Series.ewm
X_test.loc[seg_id, 'exp_Moving_average_300_mean'] =(ewma(x, span=300 ).mean() ).mean(skipna=True)
 X_test.loc[seg_id, 'exp_Moving_average_3000_mean'] = ewma(x, span=3000 ).mean().mean(skipna=True)
 X_test.loc[seg_id, 'exp_Moving_average_30000_mean'] = ewma(x, span=30000 ).mean().mean(skipna=True)
 no_of_std = 3
X_test.loc[seg_id, 'MA_700MA_std_mean'] = x.rolling(window=700 ).std().mean()
X_test.loc[seg_id,'MA_700MA_BB_high_mean'] =(X_test.loc[seg_id, 'Moving_average_700_mean'] + no_of_std * X_test.loc[seg_id, 'MA_700MA_std_mean'] ).mean()
X_test.loc[seg_id,'MA_700MA_BB_low_mean'] =(X_test.loc[seg_id, 'Moving_average_700_mean'] - no_of_std * X_test.loc[seg_id, 'MA_700MA_std_mean'] ).mean()
X_test.loc[seg_id, 'MA_400MA_std_mean'] = x.rolling(window=400 ).std().mean()
X_test.loc[seg_id,'MA_400MA_BB_high_mean'] =(X_test.loc[seg_id, 'Moving_average_700_mean'] + no_of_std * X_test.loc[seg_id, 'MA_400MA_std_mean'] ).mean()
X_test.loc[seg_id,'MA_400MA_BB_low_mean'] =(X_test.loc[seg_id, 'Moving_average_700_mean'] - no_of_std * X_test.loc[seg_id, 'MA_400MA_std_mean'] ).mean()
X_test.loc[seg_id, 'MA_1000MA_std_mean'] = x.rolling(window=1000 ).std().mean()
X_test.drop('Moving_average_700_mean', axis=1, inplace=True)
 X_test.loc[seg_id, 'iqr'] = np.subtract(*np.percentile(x, [75, 25]))
X_test.loc[seg_id, 'q999'] = np.quantile(x,0.999)
 X_test.loc[seg_id, 'q001'] = np.quantile(x,0.001)
 X_test.loc[seg_id, 'ave10'] = stats.trim_mean(x, 0.1)
 for windows in [10, 100, 1000]:
x_roll_std = x.rolling(windows ).std().dropna().values
x_roll_mean = x.rolling(windows ).mean().dropna().values
X_test.loc[seg_id, 'ave_roll_std_' + str(windows)] = x_roll_std.mean()
X_test.loc[seg_id, 'std_roll_std_' + str(windows)] = x_roll_std.std()
X_test.loc[seg_id, 'max_roll_std_' + str(windows)] = x_roll_std.max()
X_test.loc[seg_id, 'min_roll_std_' + str(windows)] = x_roll_std.min()
X_test.loc[seg_id, 'q01_roll_std_' + str(windows)] = np.quantile(x_roll_std, 0.01)
 X_test.loc[seg_id, 'q05_roll_std_' + str(windows)] = np.quantile(x_roll_std, 0.05)
 X_test.loc[seg_id, 'q95_roll_std_' + str(windows)] = np.quantile(x_roll_std, 0.95)
 X_test.loc[seg_id, 'q99_roll_std_' + str(windows)] = np.quantile(x_roll_std, 0.99)
 X_test.loc[seg_id, 'av_change_abs_roll_std_' + str(windows)] = np.mean(np.diff(x_roll_std))
X_test.loc[seg_id, 'av_change_rate_roll_std_' + str(windows)] = np.mean(np.nonzero(( np.diff(x_roll_std)/ x_roll_std[:-1])) [0])
 X_test.loc[seg_id, 'abs_max_roll_std_' + str(windows)] = np.abs(x_roll_std ).max()
X_test.loc[seg_id, 'ave_roll_mean_' + str(windows)] = x_roll_mean.mean()
X_test.loc[seg_id, 'std_roll_mean_' + str(windows)] = x_roll_mean.std()
X_test.loc[seg_id, 'max_roll_mean_' + str(windows)] = x_roll_mean.max()
X_test.loc[seg_id, 'min_roll_mean_' + str(windows)] = x_roll_mean.min()
X_test.loc[seg_id, 'q01_roll_mean_' + str(windows)] = np.quantile(x_roll_mean, 0.01)
 X_test.loc[seg_id, 'q05_roll_mean_' + str(windows)] = np.quantile(x_roll_mean, 0.05)
 X_test.loc[seg_id, 'q95_roll_mean_' + str(windows)] = np.quantile(x_roll_mean, 0.95)
 X_test.loc[seg_id, 'q99_roll_mean_' + str(windows)] = np.quantile(x_roll_mean, 0.99)
 X_test.loc[seg_id, 'av_change_abs_roll_mean_' + str(windows)] = np.mean(np.diff(x_roll_mean))
X_test.loc[seg_id, 'av_change_rate_roll_mean_' + str(windows)] = np.mean(np.nonzero(( np.diff(x_roll_mean)/ x_roll_mean[:-1])) [0])
 X_test.loc[seg_id, 'abs_max_roll_mean_' + str(windows)] = np.abs(x_roll_mean ).max()
if i < 12:
plt.subplot(6, 4, i + 1)
 plt.plot(seg['acoustic_data'])
 plt.title(seg_id)
<count_missing_values>
train_dataiterator = get_dataiterator(all_images, all_labels, config['DataLoader'], device=device )
X_test.isnull().sum().sum()<normalization>
class Attention(nn.Module):
def __init__(self, channels, reduction_attn=8, reduction_sc=2):
super().__init__()
self.channles_attn = channels // reduction_attn
self.channels_sc = channels // reduction_sc
self.conv_query = spectral_norm(nn.Conv2d(channels, self.channles_attn, kernel_size=1, bias=False))
self.conv_key = spectral_norm(nn.Conv2d(channels, self.channles_attn, kernel_size=1, bias=False))
self.conv_value = spectral_norm(nn.Conv2d(channels, self.channels_sc, kernel_size=1, bias=False))
self.conv_attn = spectral_norm(nn.Conv2d(self.channels_sc, channels, kernel_size=1, bias=False))
self.gamma = nn.Parameter(torch.zeros(1))
nn.init.orthogonal_(self.conv_query.weight.data)
 nn.init.orthogonal_(self.conv_key.weight.data)
 nn.init.orthogonal_(self.conv_value.weight.data)
 nn.init.orthogonal_(self.conv_attn.weight.data)
 def forward(self, x):
batch, _, h, w = x.size()
proj_query = self.conv_query(x ).view(batch, self.channles_attn, -1)
 proj_key = F.max_pool2d(self.conv_key(x), 2 ).view(batch, self.channles_attn, -1)
 attn = torch.bmm(proj_key.permute(0,2,1), proj_query)
 attn = F.softmax(attn, dim=1)
 proj_value = F.max_pool2d(self.conv_value(x), 2 ).view(batch, self.channels_sc, -1)
 attn = torch.bmm(proj_value, attn)
 attn = attn.view(batch, self.channels_sc, h, w)
 attn = self.conv_attn(attn)
 out = self.gamma * attn + x
return out
for col in X_test.columns:
if X_test[col].isnull().any() :
X_test.loc[X_test[col] == -np.inf, col] = means_dict[col]
X_test[col] = X_test[col].fillna(means_dict[col])
 X_test_scaled = pd.DataFrame(scaler.transform(X_test), columns=X_test.columns )<choose_model_class>
class CBN2d(nn.Module):
def __init__(self, num_features, num_conditions):
super().__init__()
self.bn = nn.BatchNorm2d(num_features, affine=False)
 self.embed = spectral_norm(nn.Conv2d(num_conditions, num_features*2, kernel_size=1, bias=False))
nn.init.orthogonal_(self.embed.weight.data)
 def forward(self, x, y):
out = self.bn(x)
 embed = self.embed(y.unsqueeze(2 ).unsqueeze(3))
gamma, beta = embed.chunk(2, dim=1)
 out =(1.0 + gamma)* out + beta
return out
n_fold = 5
folds = KFold(n_splits=n_fold, shuffle=True, random_state=11 )<split>
class GBlock(nn.Module):
def __init__(self, in_channels, out_channels, num_conditions, upsample=False):
super().__init__()
self.upsample = upsample
self.learnable_sc = in_channels != out_channels or upsample
self.conv1 = spectral_norm(nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1, bias=False))
self.conv2 = spectral_norm(nn.Conv2d(out_channels, out_channels, kernel_size=3, padding=1, bias=False))
self.cbn1 = CBN2d(in_channels, num_conditions)
 self.cbn2 = CBN2d(out_channels, num_conditions)
 if self.learnable_sc:
self.conv_sc = spectral_norm(nn.Conv2d(in_channels, out_channels, kernel_size=1, bias=False))
self.relu = nn.ReLU()
nn.init.orthogonal_(self.conv1.weight.data)
 nn.init.orthogonal_(self.conv2.weight.data)
 if self.learnable_sc:
nn.init.orthogonal_(self.conv_sc.weight.data)
 def _upsample_conv(self, x, conv):
x = F.interpolate(x, scale_factor=2, mode='nearest')
 x = conv(x)
 return x
def _residual(self, x, y):
x = self.relu(self.cbn1(x, y))
x = self._upsample_conv(x, self.conv1)if self.upsample else self.conv1(x)
 x = self.relu(self.cbn2(x, y))
x = self.conv2(x)
 return x
def _shortcut(self, x):
if self.learnable_sc:
x = self._upsample_conv(x, self.conv_sc)if self.upsample else self.conv_sc(x)
 return x
def forward(self, x, y):
return self._shortcut(x)+ self._residual(x, y )
def train_model(X=X_train_scaled, X_test=X_test_scaled, y=y_tr, params=None, folds=folds, model_type='lgb', plot_feature_importance=False, model=None):
oof = np.zeros(len(X))
prediction = np.zeros(len(X_test))
scores = []
feature_importance = pd.DataFrame()
for fold_n,(train_index, valid_index)in enumerate(folds.split(X)) :
print('Fold', fold_n, 'started at', time.ctime())
 X_train, X_valid = X.iloc[train_index], X.iloc[valid_index]
y_train, y_valid = y.iloc[train_index], y.iloc[valid_index]
if model_type == 'lgb':
model = lgb.LGBMRegressor(**params, n_estimators = 50000, n_jobs = -1)
 model.fit(X_train, y_train,
eval_set=[(X_train, y_train),(X_valid, y_valid)], eval_metric='mae',
verbose=10000, early_stopping_rounds=200)
 y_pred_valid = model.predict(X_valid)
 y_pred = model.predict(X_test, num_iteration=model.best_iteration_)
 if model_type == 'xgb':
train_data = xgb.DMatrix(data=X_train, label=y_train, feature_names=X.columns)
 valid_data = xgb.DMatrix(data=X_valid, label=y_valid, feature_names=X.columns)
 watchlist = [(train_data, 'train'),(valid_data, 'valid_data')]
model = xgb.train(dtrain=train_data, num_boost_round=20000, evals=watchlist, early_stopping_rounds=200, verbose_eval=500, params=params)
 y_pred_valid = model.predict(xgb.DMatrix(X_valid, feature_names=X.columns), ntree_limit=model.best_ntree_limit)
 y_pred = model.predict(xgb.DMatrix(X_test, feature_names=X.columns), ntree_limit=model.best_ntree_limit)
 if model_type == 'sklearn':
model = model
model.fit(X_train, y_train)
 y_pred_valid = model.predict(X_valid ).reshape(-1,)
 score = mean_absolute_error(y_valid, y_pred_valid)
 print(f'Fold {fold_n}.MAE: {score:.4f}.')
 print('')
 y_pred = model.predict(X_test ).reshape(-1,)
 if model_type == 'cat':
model = CatBoostRegressor(iterations=20000, eval_metric='MAE', **params)
 model.fit(X_train, y_train, eval_set=(X_valid, y_valid), cat_features=[], use_best_model=True, verbose=False)
 y_pred_valid = model.predict(X_valid)
 y_pred = model.predict(X_test)
 oof[valid_index] = y_pred_valid.reshape(-1,)
 scores.append(mean_absolute_error(y_valid, y_pred_valid))
prediction += y_pred
if model_type == 'lgb':
fold_importance = pd.DataFrame()
fold_importance["feature"] = X.columns
fold_importance["importance"] = model.feature_importances_
fold_importance["fold"] = fold_n + 1
feature_importance = pd.concat([feature_importance, fold_importance], axis=0)
 prediction /= n_fold
print('CV mean score: {0:.4f}, std: {1:.4f}.'.format(np.mean(scores), np.std(scores)))
 if model_type == 'lgb':
feature_importance["importance"] /= n_fold
if plot_feature_importance:
cols = feature_importance[["feature", "importance"]].groupby("feature" ).mean().sort_values(
 by="importance", ascending=False)[:50].index
best_features = feature_importance.loc[feature_importance.feature.isin(cols)]
plt.figure(figsize=(16, 12)) ;
sns.barplot(x="importance", y="feature", data=best_features.sort_values(by="importance", ascending=False)) ;
plt.title('LGB Features(avg over folds)');
return oof, prediction, feature_importance
return oof, prediction
else:
return oof, prediction<count_missing_values>
class Generator(nn.Module):
def __init__(self, latent_dim, ch, num_classes, embed_dim, use_attn=False):
super().__init__()
self.latent_dim = latent_dim
self.ch = ch
self.num_classes = num_classes
self.embed_dim = embed_dim
self.use_attn = use_attn
self.num_chunk = 5
num_latents = self.__get_num_latents()
self.embed = nn.Embedding(num_classes, embed_dim)
 self.fc = spectral_norm(nn.Linear(num_latents[0], ch*8*4*4, bias=False))
self.block1 = GBlock(ch*8, ch*8, num_latents[1], upsample=True)
 self.block2 = GBlock(ch*8, ch*4, num_latents[2], upsample=True)
 self.block3 = GBlock(ch*4, ch*2, num_latents[3], upsample=True)
 if use_attn:
self.attn = Attention(ch*2)
 self.block4 = GBlock(ch*2, ch, num_latents[4], upsample=True)
 self.bn = nn.BatchNorm2d(ch)
 self.relu = nn.ReLU()
self.conv_last = spectral_norm(nn.Conv2d(ch, 3, kernel_size=3, padding=1, bias=False))
self.tanh = nn.Tanh()
nn.init.orthogonal_(self.embed.weight.data)
 nn.init.orthogonal_(self.fc.weight.data)
 nn.init.orthogonal_(self.conv_last.weight.data)
 nn.init.constant_(self.bn.weight.data, 1.0)
 nn.init.constant_(self.bn.bias.data, 0.0)
 def __get_num_latents(self):
xs = torch.empty(self.latent_dim ).chunk(self.num_chunk)
 num_latents = [x.size(0)for x in xs]
for i in range(1, self.num_chunk):
num_latents[i] += self.embed_dim
return num_latents
def forward(self, x, y):
xs = x.chunk(self.num_chunk, dim=1)
 y = self.embed(y)
 h = self.fc(xs[0])
 h = h.view(h.size(0), self.ch*8, 4, 4)
 h = self.block1(h, torch.cat([y, xs[1]], dim=1))
h = self.block2(h, torch.cat([y, xs[2]], dim=1))
h = self.block3(h, torch.cat([y, xs[3]], dim=1))
if self.use_attn:
h = self.attn(h)
 h = self.block4(h, torch.cat([y, xs[4]], dim=1))
h = self.relu(self.bn(h))
out = self.tanh(self.conv_last(h))
return out
X_train_scaled.isnull().sum().sum()<count_missing_values>
class DBlock(nn.Module):
def __init__(self, in_channels, out_channels, downsample=False, optimized=False):
super().__init__()
self.downsample = downsample
self.optimized = optimized
self.learnable_sc = in_channels != out_channels or downsample
self.conv1 = spectral_norm(nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1, bias=False))
self.conv2 = spectral_norm(nn.Conv2d(out_channels, out_channels, kernel_size=3, padding=1, bias=False))
if self.learnable_sc:
self.conv_sc = spectral_norm(nn.Conv2d(in_channels, out_channels, kernel_size=1, bias=False))
self.relu = nn.ReLU()
nn.init.orthogonal_(self.conv1.weight.data)
 nn.init.orthogonal_(self.conv2.weight.data)
 if self.learnable_sc:
nn.init.orthogonal_(self.conv_sc.weight.data)
 def _residual(self, x):
if not self.optimized:
x = self.relu(x)
 x = self.conv1(x)
 x = self.relu(x)
 x = self.conv2(x)
 if self.downsample:
x = F.avg_pool2d(x, 2)
 return x
def _shortcut(self, x):
if self.learnable_sc:
if self.optimized:
x = self.conv_sc(F.avg_pool2d(x, 2)) if self.downsample else self.conv_sc(x)
 else:
x = F.avg_pool2d(self.conv_sc(x), 2)if self.downsample else self.conv_sc(x)
 return x
def forward(self, x):
return self._shortcut(x)+ self._residual(x )
y_tr.isnull().sum()<init_hyperparams>
class Discriminator(nn.Module):
def __init__(self, ch, num_classes, use_attn=False):
super().__init__()
self.ch = ch
self.num_classes = num_classes
self.use_attn = use_attn
self.block1 = DBlock(3, ch, downsample=True, optimized=True)
 if use_attn:
self.attn = Attention(ch)
 self.block2 = DBlock(ch, ch*2, downsample=True)
 self.block3 = DBlock(ch*2, ch*4, downsample=True)
 self.block4 = DBlock(ch*4, ch*8, downsample=True)
 self.block5 = DBlock(ch*8+1, ch*8, downsample=False)
 self.relu = nn.ReLU()
self.fc = spectral_norm(nn.Linear(ch*8, 1, bias=False))
self.embed = spectral_norm(nn.Embedding(num_classes, ch*8))
self.clf = spectral_norm(nn.Linear(ch*8, num_classes, bias=False))
nn.init.orthogonal_(self.fc.weight.data)
 nn.init.orthogonal_(self.embed.weight.data)
 nn.init.orthogonal_(self.clf.weight.data)
 def minibatch_stddev(self, x, group_size=4, eps=1e-8):
shape = x.size()
y = x.view(group_size, -1, shape[1], shape[2], shape[3])
 y -= torch.mean(y, dim=0, keepdim=True)
 y = torch.mean(y.pow(2), dim=0)
 y = torch.sqrt(y + eps)
 y = torch.mean(y, dim=[1,2,3], keepdim=True)
 y = y.repeat(group_size, 1, shape[2], shape[3])
 return torch.cat([x, y], dim=1)
 def forward(self, x, y):
h = self.block1(x)
 if self.use_attn:
h = self.attn(h)
 h = self.block2(h)
 h = self.block3(h)
 h = self.block4(h)
 h = self.block5(self.minibatch_stddev(h))
h = self.relu(h)
 h = torch.sum(h, dim=(2,3))
out = self.fc(h)
 out += torch.sum(self.embed(y)*h, dim=1, keepdim=True)
 ac = self.clf(h)
 ac = F.log_softmax(ac, dim=1)
 return out, ac
params = {'num_leaves': 128,
'min_data_in_leaf': 79,
'objective': 'huber',
'max_depth': -1,
'learning_rate': 0.01,
"boosting": "gbdt",
"bagging_freq": 5,
"bagging_fraction": 0.8126672064208567,
"bagging_seed": 11,
"metric": 'mae',
"verbosity": -1,
'reg_alpha': 0.1302650970728192,
'reg_lambda': 0.3603427518866501
}
oof_lgb, prediction_lgb, feature_importance = train_model(params=params, model_type='lgb', plot_feature_importance=True )<train_model>
netG = Generator(**config['Generator'] ).to(device, torch.float32)
 netD = Discriminator(**config['Discriminator'] ).to(device, torch.float32 )
xgb_params = {'eta': 0.03,
'max_depth': 9,
'subsample': 0.9,
'objective': 'reg:linear',
'eval_metric': 'mae',
'silent': True,
'nthread': 4}
oof_xgb, prediction_xgb = train_model(X=X_train_scaled, X_test=X_test_scaled, params=xgb_params, model_type='xgb' )<create_dataframe>
netGE = Generator(**config['Generator'] ).to(device, torch.float32)
 netGE.load_state_dict(netG.state_dict());
<save_to_csv>
optim_G = Adam(params=netG.parameters() , lr=config['lr_G'], betas=config['betas'])
 optim_D = Adam(params=netD.parameters() , lr=config['lr_D'], betas=config['betas'] )
submission['time_to_failure'] =(prediction_lgb+prediction_xgb)/2
print(submission.head())
 submission.to_csv('submission.csv' )<load_from_csv>
decay_iter = config['num_iterations'] - config['decay_start_iteration']
if decay_iter > 0:
lr_lambda_G = lambda x:(max(0,1-x/decay_iter))
lr_lambda_D = lambda x:(max(0,1-x/(decay_iter*config['d_steps'])))
 lr_sche_G = LambdaLR(optim_G, lr_lambda=lr_lambda_G)
 lr_sche_D = LambdaLR(optim_D, lr_lambda=lr_lambda_D )
%%time
train_df = pd.read_csv(".. /input/train.csv", dtype={'acoustic_data': 'int16', 'time_to_failure': 'float64'})
 gc.collect() 
<load_from_csv>
def calc_advloss_D(real, fake, margin=1.0):
loss_real = torch.mean(( real - fake.mean() - margin)** 2)
 loss_fake = torch.mean(( fake - real.mean() + margin)** 2)
 loss =(loss_real + loss_fake)/ 2
return loss
seg_lst = list(os.listdir(".. /input/test/"))
for f in seg_lst[:5]:
seg_df = pd.read_csv(f".. /input/test/{f}")
 seg_df.plot()
plt.title(f"{f}")
 plt.show()
plot_transformed(seg_df.acoustic_data)
 del seg_df
gc.collect()<load_from_csv>
def calc_advloss_G(real, fake, margin=1.0):
loss_real = torch.mean(( real - fake.mean() + margin)** 2)
 loss_fake = torch.mean(( fake - real.mean() - margin)** 2)
 loss =(loss_real + loss_fake)/ 2
return loss
c = 0
for f in seg_lst:
seg_df = pd.read_csv(f".. /input/test/{f}")
 if seg_df.max().values > 2000:
print(f"Huge fluctuations in {f}")
 seg_df.plot()
plt.title(f"{f}")
 plt.show()
plot_transformed(seg_df.acoustic_data)
 c += 1
del seg_df
if c >= 5:
break
gc.collect()<drop_column>
criterion = nn.NLLLoss().to(device, torch.float32 )
del train_df
gc.collect()<feature_engineering>
def sample_latents(batch_size, latent_dim, num_classes):
latents = torch.randn(( batch_size, latent_dim), dtype=torch.float32, device=device)
 labels = torch.randint(0, num_classes, size=(batch_size,), dtype=torch.long, device=device)
 return latents, labels
derived_trn = pd.DataFrame()
train_rd = pd.read_csv(".. /input/train.csv", iterator=True, chunksize=150000)
 def get_features(chunk):
curr_df = pd.DataFrame()
curr_df["mean"] = [chunk.acoustic_data.mean() ]
curr_df["std"] = [chunk.acoustic_data.std() ]
curr_df["kurtosis"] = [chunk.acoustic_data.kurtosis() ]
curr_df["skew"] = [chunk.acoustic_data.skew() ]
curr_df["quantile_05"], curr_df["quantile_25"], curr_df["quantile_75"], curr_df["quantile_95"] = \
[[x] for x in chunk.acoustic_data.quantile([0.05, 0.25, 0.75, 0.95])]
win_width = 100
slepian_width = 51
windowed = chunk.acoustic_data.rolling(win_width, win_type='slepian' ).mean(width=slepian_width ).dropna()
curr_df[f"window_{win_width}_mean"] = [windowed.mean() ]
curr_df[f"window_[win_width]_std"] = [windowed.std() ]
fft = np.fft.fft(chunk.acoustic_data)
 curr_df["fft_real_mean"] = [fft.real.mean() ]
curr_df["fft_image_mean"] = [fft.imag.mean() ]
curr_df["fft_real_std"] = [fft.real.std() ]
curr_df["fft_image_std"] = [fft.imag.std() ]
sig_first = chunk.acoustic_data.iloc[:20000]
curr_df["mean_first"] = [sig_first.mean() ]
curr_df["std_first"] = [sig_first.std() ]
curr_df["kurtosis_first"] = [sig_first.kurtosis() ]
curr_df["skew_first"] = [sig_first.skew() ]
curr_df["quantile_05_first"], curr_df["quantile_25_first"], \
curr_df["quantile_75_first"], curr_df["quantile_95_first"] = [[x] for x in sig_first.quantile([0.05, 0.25, 0.75, 0.95])]
windowed_first = sig_first.rolling(win_width, win_type='slepian' ).mean(width=slepian_width ).dropna()
curr_df[f"window_{win_width}_mean_first"] = [windowed_first.mean() ]
curr_df[f"window_{win_width}_std_first"] = [windowed_first.std() ]
fft_first = np.fft.fft(sig_first)
 curr_df["fft_real_mean_first"] = [fft_first.real.mean() ]
curr_df["fft_image_mean_first"] = [fft_first.imag.mean() ]
curr_df["fft_real_std_first"] = [fft_first.real.std() ]
curr_df["fft_image_std_first"] = [fft_first.imag.std() ]
sig_last = chunk.acoustic_data.iloc[-20000:]
curr_df["mean_last"] = [sig_last.mean() ]
curr_df["std_last"] = [sig_last.std() ]
curr_df["kurtosis_last"] = [sig_last.kurtosis() ]
curr_df["skew_last"] = [sig_last.skew() ]
curr_df["quantile_05_last"], curr_df["quantile_25_last"], \
curr_df["quantile_75_last"], curr_df["quantile_95_last"] = [[x] for x in sig_last.quantile([0.05, 0.25, 0.75, 0.95])]
windowed_last = sig_last.rolling(win_width, win_type='slepian' ).mean(width=slepian_width ).dropna()
curr_df[f"window_{win_width}_mean_last"] = [windowed_last.mean() ]
curr_df[f"window_{win_width}_std_last"] = [windowed_last.std() ]
fft_last = np.fft.fft(sig_last)
 curr_df["fft_real_mean_last"] = [fft_last.real.mean() ]
curr_df["fft_image_mean_last"] = [fft_last.imag.mean() ]
curr_df["fft_real_std_last"] = [fft_last.real.std() ]
curr_df["fft_image_std_last"] = [fft_last.imag.std() ]
return curr_df
for chunk in train_rd:
curr_df = get_features(chunk)
 curr_df["target"] = chunk.time_to_failure.iloc[-1]
derived_trn = pd.concat([derived_trn, curr_df], ignore_index=True)
 gc.collect()<save_to_csv>
step = 1
while True:
for i in range(config['d_steps']):
for param in netD.parameters() :
param.requires_grad_(True)
 optim_D.zero_grad()
real_imgs, real_labels = train_dataiterator.__next__()
batch_size = real_imgs.size(0)
 latents, fake_labels = sample_latents(batch_size, **config['sample_latents'])
 fake_imgs = netG(latents, fake_labels ).detach()
preds_real, preds_real_labels = netD(real_imgs, real_labels)
 preds_fake, _ = netD(fake_imgs, fake_labels)
 loss_D = calc_advloss_D(preds_real, preds_fake, config['margin'])
 loss_D += config['gamma'] * criterion(preds_real_labels, real_labels)
 loss_D.backward()
optim_D.step()
if(decay_iter > 0)and(step > config['decay_start_iteration']):
lr_sche_D.step()
for param in netD.parameters() :
param.requires_grad_(False)
 optim_G.zero_grad()
real_imgs, real_labels = train_dataiterator.__next__()
batch_size = real_imgs.size(0)
 latents, fake_labels = sample_latents(batch_size, **config['sample_latents'])
 fake_imgs = netG(latents, fake_labels)
 preds_real, _ = netD(real_imgs, real_labels)
 preds_fake, preds_fake_labels = netD(fake_imgs, fake_labels)
 loss_G = calc_advloss_G(preds_real, preds_fake, config['margin'])
 loss_G += config['gamma'] * criterion(preds_fake_labels, fake_labels)
 loss_G.backward()
optim_G.step()
if(decay_iter > 0)and(step > config['decay_start_iteration']):
lr_sche_G.step()
for param_G, param_GE in zip(netG.parameters() , netGE.parameters()):
param_GE.data.mul_(config['ema'] ).add_(( 1-config['ema'])*param_G.data)
 for buffer_G, buffer_GE in zip(netG.buffers() , netGE.buffers()):
buffer_GE.data.mul_(config['ema'] ).add_(( 1-config['ema'])*buffer_G.data)
 if step < config['num_iterations']:
step += 1
else:
print('total step: {}'.format(step))
break
derived_trn.to_csv("derived_train.csv", index=False)
 display(derived_trn.head())
 display(derived_trn.loc[(derived_trn.target <= 0.33)&(derived_trn.target >= 0.3)].head())
 features = [col for col in derived_trn.columns if col != "target"]<load_from_csv>
def truncated_normal(size, threshold=2.0, dtype=torch.float32, device='cpu'):
x = scipy.stats.truncnorm.rvs(-threshold, threshold, size=size)
 x = torch.from_numpy(x ).to(device, dtype)
 return x
test_seg_list = list(os.listdir(".. /input/test/"))
derived_test = pd.DataFrame()
for csv_file in test_seg_list:
curr_test_df = pd.read_csv(f".. /input/test/{csv_file}", dtype={"acoustic_data": "int16"})
 curr_df = get_features(curr_test_df)
 curr_df["seg_id"] = [csv_file.split(".")[0]]
derived_test = pd.concat([derived_test, curr_df], ignore_index=True)
 gc.collect()<save_to_csv>
def generate_eval_samples(generator, batch_size, latent_dim, num_classes):
latents = truncated_normal(( batch_size, latent_dim), dtype=torch.float32, device=device)
 labels = torch.randint(0, num_classes, size=(batch_size,), dtype=torch.long, device=device)
 with torch.no_grad() :
imgs =(generator(latents, labels)+ 1)/ 2
return imgs
<init_hyperparams><EOS> 
repeat_breeds = 5
nrow = 15
split_size = 50
latent_dim = config['sample_latents']['latent_dim']
num_classes = config['sample_latents']['num_classes']
all_labels = torch.arange(num_classes, dtype=torch.long, device=device)
 all_labels = all_labels.repeat_interleave(repeat_breeds)
 labels_split = all_labels.split(split_size)
 imgs_list = []
for labels in labels_split:
batch_size = labels.size(0)
 latents = truncated_normal(( batch_size, latent_dim), threshold=1.5, dtype=torch.float32, device=device)
 with torch.no_grad() :
imgs =(netGE(latents, labels)+ 1)/ 2
imgs_list.append(imgs)
 all_imgs = torch.cat(imgs_list, dim=0)
 all_imgs = make_grid(all_imgs, nrow=nrow, normalize=False)
 all_imgs = all_imgs.mul_(255 ).add_(0.5 ).clamp_(0, 255 ).permute(1, 2, 0 ).to('cpu', torch.uint8 ).numpy()
plt.figure(figsize=(2*nrow, 2*(num_classes*repeat_breeds)//nrow))
plt.imshow(all_imgs);