def modify(y_tr, x_tr, x_tst, y_tst, simple_N, noise_pattern, noise_ratio): idx0123 = (np.argmax(y_tr, axis=1) <= 3) simple_index = range(simple_N) x_tr = x_tr[idx0123, :][simple_index, :] y_tr = y_tr[idx0123, 0:4][simple_index, 0:4] idx0123_test = (np.argmax(y_tst, axis=1) <= 3) x_tst = x_tst[idx0123_test, :] y_tst = y_tst[idx0123_test, 0:4] y_tr_noisy = data.flip_label(y_tr, pattern=noise_pattern, ratio=noise_ratio, one_hot=True) return (x_tr, y_tr, x_tst, y_tst, y_tr_noisy)
## DONE ################################################################################################################################# """ Data preparation """ if dataset == 'cifar10': x_train, y_train, _, _, x_test, y_test = data.prepare_cifar10_data( data_dir='data/cifar-10-batches-py' ) ## forms the training and testing data ## DONE Num_top = 1 #using top k prediction ## DONE y_train_noisy = data.flip_label( y_train, pattern=noise_pattern, ratio=noise_ratio, one_hot=True ) ## y_train_noisy is the noisy labels where we randomly change labels of some examples input_shape = list( x_train.shape[1:]) ## shape of the given as input to the models (IMP) n_classes = y_train.shape[1] n_train = x_train.shape[0] np.save('y_train_total.npy', y_train) np.save('y_train_noisy_total.npy', y_train_noisy) clean_index = np.array([ (y_train_noisy[i, :] == y_train[i, :]).all() for i in range(n_train) ]) # For tracking only, unused during training noisy_index = np.array([not i for i in clean_index]) # we make an array which tells which of the indices are noisy and which ones are clean. ## DONE
INCV_iter = 4 epochs = 200 INCV_name = 'INCV_ResNet32' save_dir = 'saved_model' if not os.path.isdir(save_dir): os.makedirs(save_dir) filepath_INCV = os.path.join(save_dir,INCV_name+'-noisy.h5') ################################################################################################################################# """ Data preparation """ if dataset=='cifar10': x_train, y_train, _, _, x_test, y_test = data.prepare_cifar10_data(data_dir='data/cifar-10-batches-py') Num_top = 1 #using top k prediction y_train_noisy = data.flip_label(y_train, pattern=noise_pattern, ratio=noise_ratio, one_hot=True) input_shape = list(x_train.shape[1:]) n_classes = y_train.shape[1] n_train = x_train.shape[0] np.save('y_train_total.npy',y_train) np.save('y_train_noisy_total.npy',y_train_noisy) clean_index = np.array([(y_train_noisy[i,:]==y_train[i,:]).all() for i in range(n_train)])# For tracking only, unused during training noisy_index = np.array([not i for i in clean_index]) # Generator for data augmantation datagen = ImageDataGenerator(width_shift_range=4./32, # randomly shift images horizontally (fraction of total width) height_shift_range=4./32, # randomly shift images vertically (fraction of total height) horizontal_flip=True) # randomly flip images ################################################################################################################################# """ Build model """