Ejemplo n.º 1
0
    def forward(self, input, spec_aug=False, mixup_lambda=None):
        #print(input.type())                                                     # Input : (16, 144000)
        x = self.spectrogram_extractor(input.float())                            # Output : (batch_size, 1, time_steps, n_fft + 1) : (16, 1, 696, 513)
        x = self.logmel_extractor(x)                                             # Output : (batch_size, 1, time_steps, mel_bins)     : (16, 1 , 696, 128)
        frames_num = x.shape[2]
        
        if self.training:
            x = self.spec_augmenter(x)
        
        # Mixup on spectrogram
        if mixup_lambda is not None:
            x = do_mixup(x, mixup_lambda)
        
        x = x.transpose(1, 3)
        x = self.batch_norm(x)
        x = x.transpose(1, 3)
                                                                                  # (16, 1, 2087, 128)
        x = self.encoder.forward_features(x)                                     # output : (batch_size, n_features, 66, 4)
        # Aggregate in time axis
        x = torch.mean(x, dim=3)                                                 # (16, 2048, 22) : (batch_size, n_features, _)
        x1 = F.max_pool1d(x, kernel_size=3, stride=1, padding=1)
        x2 = F.avg_pool1d(x, kernel_size=3, stride=1, padding=1)
        x = x1 + x2                                                              # (16, 2048, 22)

        x = F.dropout(x, p=0.5, training=self.training)
        x = x.transpose(1, 2)                                                    # (batch_size, 22, n_features)
        #x = self.encoder.classifier(x)                                                    # (16, 22, 2048) : (batch_size, time, n_features)
        x = F.relu_(self.encoder.fc(x))                                          # (16, 22, 2048)
        x = x.transpose(1, 2)                                                    # (16, 2048, 22)
        x = F.dropout(x, p=0.5, training=self.training)

        (clipwise_output, norm_att, segmentwise_output) = self.att_head(x)
        segmentwise_output = segmentwise_output.transpose(1, 2)
        #print("clipwise_output.size : {}".format(clipwise_output.size()))        #(16, 24)     : (batch_sizes, n_features)
        #print("norm_att.size : {}".format(norm_att.size()))                      #(16, 24, 22) : (batch_sizes, n_features, time)
        #print("segmentwise_output.size : {}".format(segmentwise_output.size()))  #(16, 24, 22) : (batch_sizes, n_features, time)
        
        #Upscale back to original size
        framewise_output = interpolate(segmentwise_output,
                                       self.interpolate_ratio)                    # (16,696, 24) : (batch_sizes x time x num_classes)

        framewise_output = pad_framewise_output(framewise_output, frames_num)     # (16,696, 24) : (batch_sizes x time x num_classes)
        output_dict = {
           'framewise_output': framewise_output,
            'clipwise_output': clipwise_output
        }

        return output_dict
def preprocess_data():
    print('Starting the ETL process')

    start_time = time.time()
    df_train,df_sub = ETL_emb()
    test_lengths = count_test_period(df_sub)
    elapsed_time = time.time() - start_time 

    print('Time taken to complete the ETL process {}'.format(elapsed_time))

    forecast_ids = list(test_lengths.keys())
    site_ids = list(df_sub.SiteId.unique())

    # suppress pandas warnings
    # do not do this in production!
    pd.set_option('mode.chained_assignment', None)

    submission_df = pd.read_csv('../data/forecast/submission_format.csv')
    submission_df = submission_df.set_index('obs_id')

    data_df = preprocess_seq2seq(df_train,df_sub)
    # reset index to make sure we do not have double indexes and for easier indexing
    data_df = data_df.reset_index()
    del data_df['index']

    # leave only the first holiday
    data_df = data_df[(data_df['obs_id'].shift(+1) != data_df['obs_id'])]
    data_df = data_df.reset_index()
    del data_df['index']
    # fill days wo holidays with -1
    data_df['Holiday'] = data_df['Holiday'].fillna(value=-1)

    data_df,train_forecast_ids,normal_forecast_ids,linear_interpolation,last_window,submit_zeroes,submit_averages = interpolate(data_df)
    
    return data_df,train_forecast_ids,normal_forecast_ids,linear_interpolation,last_window,submit_zeroes,submit_averages
    deformation = sdn.predict(brain_test)

    flip = np.load(LRpath + test_label[:2] + 'LR.npy')
    X = flip[..., 0].flatten()
    Y = flip[..., 1].flatten()
    Z = flip[..., 2].flatten()

    #    Xnii = sitk.ReadImage(dispath+'S'+test_label[:2]+'to' +'S'+ test_label[2:4]+'.fsl152.skullstripped_DispField_Trg2Tpl_X.nii')
    #    Ynii = sitk.ReadImage(dispath+'S'+test_label[:2]+'to' +'S'+ test_label[2:4]+'.fsl152.skullstripped_DispField_Trg2Tpl_Y.nii')
    #    Znii = sitk.ReadImage(dispath+'S'+test_label[:2]+'to' +'S'+ test_label[2:4]+'.fsl152.skullstripped_DispField_Trg2Tpl_Z.nii')

    #    dispX = sitk.GetArrayViewFromImage(Xnii)
    #    dispY = sitk.GetArrayViewFromImage(Ynii)
    #    dispZ = sitk.GetArrayViewFromImage(Znii)
    #    deformation = np.stack([dispX, dispY, dispZ], 3)
    #    deformation = np.expand_dims(deformation, 0)

    disp_LR = interpolate(deformation, Y, Z, X, (91, 109, 91))
    disp_LR = np.reshape(disp_LR, (91, 109, 91, 3))

    disp_pf = np.einsum('abcde,abce->abcd', Jac(flip),
                        disp_LR[:-1, :-1, :-1, :])
    disp_N = np.mean(deformation**2)
    disp_LR_N = 0.25 * np.mean((deformation[0, :-1, :-1, :-1, :] + disp_pf)**2)
    test_log.write('{}\t'.format(test_label))
    test_log.write('{:6f}\t'.format(disp_N))
    test_log.write('{:6f}\n'.format(disp_LR_N))
    print('Test sample {}\'s evaluation completed.'.format(test_ind + 1))
    #print([disp_N, disp_LR_N])
test_log.close()