def main(): datadim = 3 * 224 * 224 classdim = 1000 # PaddlePaddle init paddle.init(use_gpu=True, trainer_count=1) image = paddle.layer.data(name="image", type=paddle.data_type.dense_vector(datadim)) #net = mobile_net(image) net = Alexnet(image) out = paddle.layer.fc(input=net, size=classdim, act=paddle.activation.Softmax()) ''' out = paddle.layer.img_conv( input=net, filter_size=1, num_filters=classdim, stride=1, act=paddle.activation.Linear()) ''' return out
def __init__(self, config): ### Initialize setting log('setup', 'Initializing network') np.set_printoptions(precision=4) self.device = config['device'] self.batch_size = config['batch_size'] self.n_class = config['n_class'] self.hash_bit_num = config['hash_bit_num'] self.model_path = config['model_path'] self.mean_file = '../data/imagenet_mean.npy' ### Setup session log('setup', 'Launching session') configProto = tf.ConfigProto() configProto.gpu_options.allow_growth = True configProto.allow_soft_placement = True self.sess = tf.Session(config=configProto) ### Construct network structure log('setup', 'Creating network') with tf.device(self.device): ### Setup inputs self.test_img = tf.placeholder(tf.float32, [self.batch_size, 256, 256, 3]) ### Construct CNN self.cnn = Alexnet(self.model_path, self.hash_bit_num, self.sess) ### Construct train net test_img = self.preprocess_img(self.test_img, self.batch_size, False) with tf.variable_scope("cnn"): feature = self.cnn.extract(test_img) tf.get_variable_scope().reuse_variables() self.hash_bit = tf.tanh(feature) tf.get_variable_scope().reuse_variables() ### init all variables log('setup', 'Initialize all variables')
def main(): parser = argparse.ArgumentParser() parser.add_argument("--data", help="cifar data path", default="../data/cifar-10-batches-py") parser.add_argument("--epochs", type=int, help="number of learning epoch, default is 10", default=10) parser.add_argument("--saving", help="wheter saving or not(each verbose iteration)", action="store_true") parser.add_argument("--batch_size", type=int, help="batch size(default is 32)", default=32) parser.add_argument("--verbose", type=int, help="verbosity cycle(default is 1 epoch)", default=1) parser.add_argument("--no_tqdm", help="whether to use tqdm process bar", action="store_true") parser.add_argument("--lr", type=float, help="learning rate, default is 0.001", default=1e-3) args = parser.parse_args() dirname = args.data X_train, y_train = preprocess_train(dirname) X_test, y_test = preprocess_test(dirname) device = 'gpu:0' if tfe.num_gpus() > 0 else 'cpu:0' alex_model = Alexnet(learning_rate=args.lr, device_name=device) alex_model(tf.convert_to_tensor(X_train[:1]), True) alex_model.summary() # alex_model.load() - for loading your latest saved model if args.no_tqdm: tqdm_option = None else: tqdm_option = "normal" alex_model.fit(X_train, y_train, X_test, y_test, epochs=args.epochs, verbose=args.verbose, batch_size=args.batch_size, saving=args.saving, tqdm_option=tqdm_option)
#importing packages import numpy as np from alexnet import Alexnet #input np.random.seed(0) x=np.random.uniform(size=(3,227,227)) emp=Alexnet.function(x) print(emp)
class Net(object): def __init__(self, config): ### Initialize setting log('setup', 'Initializing network') np.set_printoptions(precision=4) self.device = config['device'] self.batch_size = config['batch_size'] self.n_class = config['n_class'] self.hash_bit_num = config['hash_bit_num'] self.model_path = config['model_path'] self.mean_file = '../data/imagenet_mean.npy' ### Setup session log('setup', 'Launching session') configProto = tf.ConfigProto() configProto.gpu_options.allow_growth = True configProto.allow_soft_placement = True self.sess = tf.Session(config=configProto) ### Construct network structure log('setup', 'Creating network') with tf.device(self.device): ### Setup inputs self.test_img = tf.placeholder(tf.float32, [self.batch_size, 256, 256, 3]) ### Construct CNN self.cnn = Alexnet(self.model_path, self.hash_bit_num, self.sess) ### Construct train net test_img = self.preprocess_img(self.test_img, self.batch_size, False) with tf.variable_scope("cnn"): feature = self.cnn.extract(test_img) tf.get_variable_scope().reuse_variables() self.hash_bit = tf.tanh(feature) tf.get_variable_scope().reuse_variables() ### init all variables log('setup', 'Initialize all variables') #self.sess.run(tf.global_variables_initializer()) def test(self, test_set): log('test', 'testing starts') n_samples = test_set._n_samples # modified by caozhangjie #codes = np.empty((0, self.hash_bit_num)) #labels = np.empty((0, self.n_class)) codes = [] labels = [] samples = 0 while 1: test_img, test_label, has_next = test_set.next_batch( self.batch_size) hash_bit = self.sess.run(self.hash_bit, feed_dict={self.test_img: test_img}) #print hash_bit if has_next: samples += self.batch_size # modified by caozhangjie #codes = np.append(codes, hash_bit, axis = 0) #labels = np.append(labels, test_label, axis = 0) codes.append(hash_bit) labels.append(test_label) else: rest_num = n_samples - samples # modified by caozhangjie #codes = np.append(codes, hash_bit[0:rest_num, :], axis = 0) #labels = np.append(labels, test_label[0:rest_num, :], axis = 0) codes.append(hash_bit[0:rest_num, :]) labels.append(test_label[0:rest_num, :]) print 'done!' break print str(samples) + '/' + str(n_samples) codes = np.vstack(codes) labels = np.vstack(labels) assert codes.shape[0] == n_samples, "sample num error" assert labels.shape[0] == n_samples, "sample num error" return codes, labels def preprocess_img(self, img, batch_size, train_phase, oversample=False): ''' pre-process input image: Args: img: 4-D tensor batch_size: Int train_phase: Bool Return: distorted_img: 4-D tensor ''' reshaped_image = tf.cast(img, tf.float32) mean = tf.constant(np.load(self.mean_file), dtype=tf.float32, shape=[1, 256, 256, 3]) reshaped_image -= mean crop_height = IMAGE_SIZE crop_width = IMAGE_SIZE if train_phase: distorted_img = tf.stack([ tf.random_crop(tf.image.random_flip_left_right(each_image), [crop_height, crop_width, 3]) for each_image in tf.unstack(reshaped_image) ]) else: if oversample: distorted_img1 = tf.stack([ tf.image.crop_to_bounding_box( tf.image.flip_left_right(each_image), 0, 0, crop_height, crop_width) for each_image in tf.unstack(reshaped_image) ]) distorted_img2 = tf.stack([ tf.image.crop_to_bounding_box( tf.image.flip_left_right(each_image), 28, 28, crop_height, crop_width) for each_image in tf.unstack(reshaped_image) ]) distorted_img3 = tf.stack([ tf.image.crop_to_bounding_box( tf.image.flip_left_right(each_image), 28, 0, crop_height, crop_width) for each_image in tf.unstack(reshaped_image) ]) distorted_img4 = tf.stack([ tf.image.crop_to_bounding_box( tf.image.flip_left_right(each_image), 0, 28, crop_height, crop_width) for each_image in tf.unstack(reshaped_image) ]) distorted_img5 = tf.stack([ tf.image.crop_to_bounding_box( tf.image.flip_left_right(each_image), 14, 14, crop_height, crop_width) for each_image in tf.unstack(reshaped_image) ]) distorted_img6 = tf.stack([ tf.image.crop_to_bounding_box(each_image, 0, 0, crop_height, crop_width) for each_image in tf.unstack(reshaped_image) ]) distorted_img7 = tf.stack([ tf.image.crop_to_bounding_box(each_image, 28, 28, crop_height, crop_width) for each_image in tf.unstack(reshaped_image) ]) distorted_img8 = tf.stack([ tf.image.crop_to_bounding_box(each_image, 28, 0, crop_height, crop_width) for each_image in tf.unstack(reshaped_image) ]) distorted_img9 = tf.stack([ tf.image.crop_to_bounding_box(each_image, 0, 28, crop_height, crop_width) for each_image in tf.unstack(reshaped_image) ]) distorted_img0 = tf.stack([ tf.image.crop_to_bounding_box(each_image, 14, 14, crop_height, crop_width) for each_image in tf.unstack(reshaped_image) ]) distorted_img = tf.concat(0, [ distorted_img1, distorted_img2, distorted_img3, distorted_img4, distorted_img5, distorted_img6, distorted_img7, distorted_img8, distorted_img9, distorted_img0 ]) else: distorted_img = tf.stack([ tf.image.crop_to_bounding_box(each_image, 14, 14, crop_height, crop_width) for each_image in tf.unstack(reshaped_image) ]) return distorted_img
model = VGG_interpretable(num_classes=num_classe) elif args.model_type == 'ex_gradcam': model = Mobile_interpretable_gradcam(num_classes=num_classe) elif args.model_type == 'ex_gradcam2': model = VGG_interpretable_gradcam2(num_classes=num_classe) else: model = MobileNet(num_classes=num_classe) elif args.model == 'alexnet': if args.model_type == 'ex_atten': model = VGG_interpretable_atten(num_classes=num_classe) elif args.model_type == 'ex': model = Alexnet_interpretable(num_classes=num_classe) elif args.model_type == 'ex_gradcam': model = Alexnet_interpretable_gradcam(num_classes=num_classe) else: model = Alexnet(num_classes=num_classe) use_gpu = torch.cuda.is_available() # 判断是否有GPU加速 if use_gpu: model = model.cuda() if model_half: model = model.half() if args.model_init: for m in model.modules(): if isinstance(m, (nn.Conv2d, nn.Linear)): nn.init.xavier_uniform_(m.weight) '''定义loss和optimizer''' criterion = nn.CrossEntropyLoss() base_params = list(map(id, model.base_model.parameters())) logits_params = filter(lambda p: id(p) not in base_params, model.parameters()) params = [
aug = ImageDataGenerator(rotation_range=20, zoom_range=0.15, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.15, horizontal_flip=True, fill_mode="nearest") means = json.loads(open(config.DATASET_MEAN).read()) sp = SimplePreprocessor(227, 227) pp = PatchPreprocessor(227, 227) mp = MeanPreprocessor(means["R"], means["G"], means["B"]) iap = ImageToArrayPreprocessor() trainGen = HDF5DatasetGenerator(config.TRAIN_HDF5, 128, aug=aug, preprocessors=[pp, mp, iap], classes=2) valGen = HDF5DatasetGenerator(config.VAL_HDF5, 128, aug=aug, preprocessors=[sp, mp, iap], classes=2) print("[INFO] compiling model...") opt = Adam(lr=1e-3) model = Alexnet.build(width=227, height=227, depth=3, classes=2, reg=0.0002) model.compile(loss="binary_crossentropy", optimizer=opt, metrics=["accuracy"]) path = os.path.sep.join([config.OUTPUT_PATH, "{}.png".format(os.getpid())]) callbacks = [TrainingMonitor(path)] model.fit_generator(trainGen.generator(), steps_per_epoch=trainGen.numImages // 128, validation_data=valGen.generator(), validation_steps=valGen.numImages // 128, epochs=75, max_queue_size=128*2, callbacks=callbacks, verbose=1) print("[INFO] serializing model...") model.save(config.MODEL_PATH, overwrite=True) trainGen.close() valGen.close()
from alexnet import Alexnet from keras.utils import plot_model from keras.applications import ResNet50 from keras.applications import VGG16 alexnet_model = Alexnet.build(227, 227, 3, 2) plot_model(alexnet_model, to_file="alexnet_model-structure.png", show_shapes=True) resnet_model = ResNet50(weights="imagenet", include_top=False) plot_model(resnet_model, to_file="resnet50_model-structure.png", show_shapes=True) vgg_model = VGG16(weights="imagenet", include_top=False) plot_model(vgg_model, to_file="vgg16_model-structure.png", show_shapes=True)
def train(model_save_dir, dataset_dir, batch_size, lr, num_worker, num_epoch, load_model_dir): # Initialize a list of transformation transform = transforms.Compose([ transforms.RandomHorizontalFlip(), transforms.RandomResizedCrop(224), transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) ]) # Set CIFAR10 Dataset train_dataset = datasets.CIFAR10(dataset_dir, train=True, download=True, transform=transform) # Set CIFAR10 DataLoader train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True, num_workers=num_worker) # Set Alexnet Model model = Alexnet(10) if load_model_dir is not None: model.load_state_dict(torch.load(load_model_dir)) model.cuda() loss_func = torch.nn.CrossEntropyLoss() optimizer = torch.optim.Adam(model.parameters(), lr=lr) model.train() loss_lis = [] for epoch in range(num_epoch): tot_loss = 0 for batch_idx, (x, y) in enumerate(train_loader): x = Variable(x.cuda()) y = Variable(y.cuda()) optimizer.zero_grad() output = model(x) loss = loss_func(output, y) loss.backward() optimizer.step() tot_loss = tot_loss + loss.data.item() if batch_idx % 100 == 0: print( 'Train Epoch: {} {:.2f}% Percent Finished. Current Loss: {:.6f}' .format(epoch + 1, 100 * batch_idx / len(train_loader), tot_loss)) print('Epoch {} Finished! Total Loss: {:.2f}'.format( epoch + 1, tot_loss)) loss_lis.append(tot_loss) torch.save(model.state_dict(), model_save_dir) x = [x + 1 for x in range(num_epoch)] plt.plot(x, loss_lis) plt.xlabel("Epoch Number") plt.ylabel("Total Loss Per Epoch") plt.show()
data_loc = '...' #validation data directory data_test_loc = '...' #test data directory training_set = Dataset(data_loc) #Or can use torchvision dataset #training_set = torchvision.datasets.CIFAR100('.', train=True, download=True) training_generator = DataLoader(training_set, batch_size=128) validation_set = Dataset(data_test_loc) validation_generator = DataLoader(validation_set, batch_size=128) model = Alexnet() #Data Parallelism, if multiple gpu available if torch.cuda.device_count() > 1: model = nn.DataParallel(model) #use gpu, if available if use_cuda: model.to(device) #Loss and hyperparameters set as per the authors of Alexnet criterion = nn.CrossEntropyLoss() learning_rate = 0.0005 optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate, momentum=0.9) accuracy_list, loss_list = train_model(model=model,
def train(arc): print("Loading images...") imagePaths = sorted(list(paths.list_images("train"))) ground_truth = open("new_ground_truth.txt") random.seed(42) random.shuffle(imagePaths) data = [] labels = [] data_test = [] data_labels = [] for imagePath in imagePaths: image = cv2.imread(imagePath) image = cv2.resize(image, (IMAGE_DIMS[1], IMAGE_DIMS[0])) image = img_to_array(image) data.append(image) l = imagePath.split(os.path.sep)[-2].split("_")[0].replace('\n', '') labels.append(l) data = np.array(data, dtype="float") / 255.0 labels = np.array(labels) lb = LabelBinarizer() labels = lb.fit_transform(labels) test_images = os.listdir("test") for line in ground_truth.readlines(): image_path = line.split("\t")[0] if image_path in test_images: image = cv2.imread("test/" + image_path) image = cv2.resize(image, (IMAGE_DIMS[1], IMAGE_DIMS[0])) image = img_to_array(image) data_test.append(image) l = label = line.split("\t")[1].replace("\n", "") data_labels.append(l) data_test = np.array(data_test, dtype="float") / 255.0 data_labels = np.array(data_labels) lb2 = LabelBinarizer() data_labels = lb2.fit_transform(data_labels) trainX = data trainY = labels testX = data_test testY = data_labels #(trainX, testX, trainY, testY) = train_test_split(data, labels, test_size=0.2, random_state=42) aug = ImageDataGenerator(rotation_range=25, width_shift_range=0.1, height_shift_range=0.1, shear_range=0.2, zoom_range=0.2, horizontal_flip=True, fill_mode="nearest") if arc == "vgg": model = SmallerVGGNet.build(width=IMAGE_DIMS[1], height=IMAGE_DIMS[0], depth=IMAGE_DIMS[2], classes=len(lb.classes_)) else: model = Alexnet.build(width=IMAGE_DIMS[1], height=IMAGE_DIMS[0], depth=IMAGE_DIMS[2], classes=len(lb.classes_)) opt = Adam(lr=INIT_LR, decay=INIT_LR / EPOCHS) plot_model(model, to_file='model.png') model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"]) print("Training the network...") H = model.fit_generator(aug.flow(trainX, trainY, batch_size=BS), validation_data=(testX, testY), steps_per_epoch=len(trainX) // BS, epochs=EPOCHS, verbose=1) if arc == "vgg": model.save("modVgg.model") else: model.save("modAlex.model") f = open("lb.pickle", "wb") f.write(pickle.dumps(lb)) f.close() plt.style.use("ggplot") plt.figure() N = EPOCHS plt.plot(np.arange(0, N), H.history["loss"], label="train_loss") plt.plot(np.arange(0, N), H.history["val_loss"], label="val_loss") plt.plot(np.arange(0, N), H.history["acc"], label="train_acc") plt.plot(np.arange(0, N), H.history["val_acc"], label="val_acc") plt.title("Training Loss and Accuracy") plt.xlabel("Epoch #") plt.ylabel("Loss/Accuracy") plt.legend(loc="upper left") plt.savefig("plot1.png")