def train(train_loader, dataset, Numlayers): """i=0 for images, labels in train_loader: # test set 批处理 if i==0: images_train= np.swapaxes(images.numpy(),3,1) y_train=labels.numpy() i+=1 else: images_train= np.concatenate((images_train,np.swapaxes(images.numpy(),3,1)),axis=0) y_train=np.concatenate((y_train,labels.numpy()),axis=0) """ N = 10 if dataset == 'MNIST': imageShape = 28 elif dataset == 'CIFAR10': imageShape = 32 images_train = np.random.randn(N, imageShape, imageShape, 1) y_train = np.random.randint(2, size=(N, 1)) print("Training PCANet") if dataset == 'MNIST': pcanet = net.PCANet(image_shape=28, filter_shape_l1=2, step_shape_l1=1, n_l1_output=3, filter_shape_l2=2, step_shape_l2=1, n_l2_output=3, filter_shape_pooling=2, step_shape_pooling=2) elif dataset == 'CIFAR10': pcanet = net.PCANet(image_shape=32, filter_shape_l1=5, step_shape_l1=1, n_l1_output=16, filter_shape_l2=5, step_shape_l2=1, n_l2_output=8, filter_shape_pooling=8, step_shape_pooling=4) pcanet.validate_structure() print("Training the classifier") for i in range(Numlayers): pcanet.fit(images_train) X_train = pcanet.transform(images_train) images_train = SVC(C=10, gamma='auto') classifier.fit(X_train, y_train.ravel()) return pcanet, classifier
def train(train_set): images_train, y_train = train_set print("Training PCANet") pcanet = net.PCANet(image_shape=28, filter_shape_l1=2, step_shape_l1=1, n_l1_output=3, filter_shape_l2=2, step_shape_l2=1, n_l2_output=3, filter_shape_pooling=2, step_shape_pooling=2) pcanet.validate_structure() t1 = timeit.default_timer() pcanet.fit(images_train) t2 = timeit.default_timer() train_time = t2 - t1 t1 = timeit.default_timer() X_train = pcanet.transform(images_train) t2 = timeit.default_timer() transform_time = t2 - t1 print("Training the classifier") classifier = SVC(C=10) classifier.fit(X_train, y_train) return pcanet, classifier
def train(train_set): images_train, y_train = train_set print("Training PCANet") pcanet = net.PCANet(image_shape=28, filter_shape_l1=2, step_shape_l1=1, n_l1_output=3, filter_shape_l2=2, step_shape_l2=1, n_l2_output=3, filter_shape_pooling=2, step_shape_pooling=2) pcanet.validate_structure() t1 = timeit.default_timer() pcanet.fit(images_train) t2 = timeit.default_timer() train_time = t2 - t1 print("fit time comsumed:", train_time / 60, " min") t1 = timeit.default_timer() print("images_train:", images_train.shape) X_train = pcanet.transform(images_train) print("X_train transform:", X_train.shape) t2 = timeit.default_timer() transform_time = t2 - t1 print("transform time comsumed:", transform_time / 60, " min") print("Training the classifier") # X_train = images_train.reshape(images_train.shape[0],-1) classifier = SVC(C=10) t1 = timeit.default_timer() classifier.fit(X_train, y_train) t2 = timeit.default_timer() classifier_time = t2 - t1 print("classifier time comsumed:", classifier_time / 60, " min") return pcanet, classifier
def train(): print("Training PCANet") pcanet = net.PCANet( image_shape=(240,256), filter_shape_l1=2, step_shape_l1=1, n_l1_output=3, filter_shape_l2=2, step_shape_l2=1, n_l2_output=3, filter_shape_pooling=2, step_shape_pooling=2 ) #clf = SGDClassifier(loss="hinge", penalty="l2", max_iter=5) clf=MiniBatchKMeans(n_clusters=2,random_state=0,batch_size=27808) val = int(input("Which phase you want to train?? 1,2,3,4")) if val==1: print("TRAINING PHASE 1") for i in range(0,30): images_train=load_data_negative(i) print("Input Structure-") print(images_train.shape) pcanet.validate_structure() pcanet.fit(images_train) print("Transform") X_train= pcanet.transform(images_train) print("Resultant shape after the transform") print(X_train.shape) len1=X_train.shape[0] label1=np.zeros((len1,), dtype=int) clf=clf.partial_fit(X_train) print("Resultant of transform") print(X_train[10,0:30]) save_model(pcanet,"/home/ganesh/pcanet.pkl") print("Phase 1 model saved") save_model(clf,"/home/ganesh/clf.pkl") print("Phase 1 classifier saved") elif val==2: print("TRAINING PHASE 2") print("Loading the exsisting model and Classifier") clf=load_model("/home/ganesh/clf.pkl") pcanet = load_model("/home/ganesh/pcanet.pkl") for i in range(60,120): images_train=load_data_negative(i) print("Input Structure-") print(images_train.shape) pcanet.validate_structure() pcanet.fit(images_train) print("Transform") X_train= pcanet.transform(images_train) print("Resultant shape after the transform") print(X_train.shape) len1=X_train.shape[0] label1=np.zeros((len1,), dtype=int) clf=clf.partial_fit(X_train) save_model(pcanet,"/home/ganesh/pcanet.pkl") print("Phase 2 model saved") save_model(clf,"/home/ganesh/clf.pkl") print("Phase 2 classifier saved") elif val==3: print("TRAINING PHASE 3") print("Loading the exsisting model and Classifier") clf=load_model("/home/ganesh/clf.pkl") pcanet = load_model("/home/ganesh/pcanet.pkl") for i in range(0,30): images_train=load_data_positive(i) print("Input Structure-") print(images_train.shape) pcanet.validate_structure() pcanet.fit(images_train) print("Transform") X_train= pcanet.transform(images_train) print("Resultant shape after the transform") print(X_train.shape) len1=X_train.shape[0] label1=np.ones((len1,), dtype=int) clf=clf.partial_fit(X_train) save_model(pcanet,"/home/ganesh/pcanet.pkl") print("Phase 3 model saved") save_model(clf,"/home/ganesh/clf.pkl") print("Phase 3 classifier saved") elif val==4: print("Loading the exsisting model and Classifier") clf=load_model("/home/ganesh/clf.pkl") pcanet = load_model("/home/ganesh/pcanet.pkl") print("TRAINING PHASE 4") for i in range(158,316): images_train=load_data_positive(i) print("Input Structure-") print(images_train.shape) pcanet.validate_structure() pcanet.fit(images_train) print("Transform") X_train= pcanet.transform(images_train) print("Resultant shape after the transform") print(X_train.shape) len1=X_train.shape[0] label1=np.ones((len1,), dtype=int) clf=clf.partial_fit(X_train) print("Resultant of transform") print(X_train[10,0:30]) save_model(pcanet,"/home/ganesh/pcanet.pkl") print("Phase 4 model saved") save_model(clf,"/home/ganesh/clf.pkl") print("Phase 4 classifier saved")