Best Python code snippet using tox_python
CatDogBuildDataset.py
Source:CatDogBuildDataset.py
1# -*- coding: utf-8 -*-2'''3æ°æ®éå è½½4'''5# @Time : 2021/4/8 22:58 6# @Author : LINYANZHEN7# @File : CatDogBuildDataset.py8import torch9from torch.utils.data import Dataset10import cv211import random12import os13import numpy as np14def cat_dog_build_dataset(dataset_path):15 l = [i for i in range(100)]16 train_set_data = np.zeros((140, 3, 128, 128))17 test_set_data = np.zeros((60, 3, 128, 128))18 train_set_label = np.zeros((140))19 test_set_label = np.zeros((60))20 random.shuffle(l)21 # å70å¼ ç«ä½ä¸ºè®ç»é22 for i in range(70):23 image = cv2.imread(os.path.join(dataset_path, "cat.{}.jpg".format(l[i])))24 image = cv2.resize(image, (128, 128))25 r = image[:, :, 2]26 g = image[:, :, 1]27 b = image[:, :, 0]28 train_set_label[i] = 029 train_set_data[i, 0, :, :] = r30 train_set_data[i, 1, :, :] = g31 train_set_data[i, 2, :, :] = b32 # å©ä¸30å¼ ç«ä½ä¸ºæµè¯é33 for i in range(70, 100):34 image = cv2.imread(os.path.join(dataset_path, "cat.{}.jpg".format(l[i])))35 image = cv2.resize(image, (128, 128))36 r = image[:, :, 2]37 g = image[:, :, 1]38 b = image[:, :, 0]39 test_set_label[i - 70] = 040 test_set_data[i - 70, 0, :, :] = r41 test_set_data[i - 70, 1, :, :] = g42 test_set_data[i - 70, 2, :, :] = b43 random.shuffle(l)44 # åéæºå70å¼ ç45 for i in range(70):46 image = cv2.imread(os.path.join(dataset_path, "dog.{}.jpg".format(l[i])))47 image = cv2.resize(image, (128, 128))48 r = image[:, :, 2]49 g = image[:, :, 1]50 b = image[:, :, 0]51 train_set_label[i + 70] = 152 train_set_data[i + 70, 0, :, :] = r53 train_set_data[i + 70, 1, :, :] = g54 train_set_data[i + 70, 2, :, :] = b55 # å©ä¸30å¼ çä½ä¸ºæµè¯é56 for i in range(70, 100):57 image = cv2.imread(os.path.join(dataset_path, "dog.{}.jpg".format(l[i])))58 image = cv2.resize(image, (128, 128))59 r = image[:, :, 2]60 g = image[:, :, 1]61 b = image[:, :, 0]62 test_set_label[i - 70 + 30] = 163 test_set_data[i - 70 + 30, 0, :, :] = r64 test_set_data[i - 70 + 30, 1, :, :] = g65 test_set_data[i - 70 + 30, 2, :, :] = b66 print("train_set_data", train_set_data)67 print("test_set_data", test_set_data)68 print("train_set_label", train_set_label)69 print("test_set_label", test_set_label)70 np.save("train_set_data", train_set_data)71 np.save("test_set_data", test_set_data)72 np.save("train_set_label", train_set_label)73 np.save("test_set_label", test_set_label)74class CDDataset(Dataset):75 def __init__(self, mode="train"):76 super(CDDataset, self).__init__()77 self.labels = []78 self.images = []79 self.len = 080 if mode == "train":81 # æ¯è®ç»é82 self.labels = torch.tensor(np.load("train_set_label.npy"), dtype=torch.long)83 self.images = torch.tensor(np.load("train_set_data.npy") / 256.0, dtype=torch.float32)84 self.len = len(self.labels)85 elif mode == "test":86 self.labels = torch.tensor(np.load("test_set_label.npy"), dtype=torch.long)87 self.images = torch.tensor(np.load("test_set_data.npy") / 256.0, dtype=torch.float32)88 self.len = len(self.labels)89 def __getitem__(self, index):90 return self.images[index, :, :, :], self.labels[index]91 def __len__(self):92 return self.len93if __name__ == '__main__':...
main.py
Source:main.py
1import sys2import theano3import numpy as np4from cnn import *5from libparser import *6# theano.config.exception_verbosity='high'7if __name__=='__main__':8 if len(sys.argv)!=2:9 print 'Usage: python main.py <dataset>(cifar-10 or svhm)'10 exit(0)11 else:12 path=sys.argv[1]+'/'13 train=unzip(path+'train_data')14 validate=unzip(path+'validate_data')15 test=unzip(path+'test_data')16 train_data=np.asarray(train['data'],dtype=theano.config.floatX)17 train_label=np.asarray(train['labels'],dtype=theano.config.floatX)18 validate_data=np.asarray(validate['data'],dtype=theano.config.floatX)19 validate_label=np.asarray(validate['labels'],dtype=theano.config.floatX)20 test_data=np.asarray(test['data'],dtype=theano.config.floatX)21 test_label=np.asarray(test['labels'],dtype=theano.config.floatX)22 machine=model(23 learn_rate=0.005,24 n_epochs=100,25 filters=[128,128,128,256,256],26 batch_size=2527 )28 train_set_data=np.asarray(train_data,dtype=theano.config.floatX)29 train_set_label=np.asarray(train_label,dtype=int)30 validate_set_data=np.asarray(validate_data,dtype=theano.config.floatX)31 validate_set_label=np.asarray(validate_label,dtype=int)32 test_set_data=np.asarray(test_data,dtype=theano.config.floatX)33 test_set_label=np.asarray(test_label,dtype=int)34 for i in xrange(len(train_set_data)):35 train_set_data[i]-=train_set_data[i].mean()36 for i in xrange(len(validate_set_data)):37 validate_set_data[i]-=validate_set_data[i].mean()38 for i in xrange(len(test_set_data)):39 test_set_data[i]-=test_set_data[i].mean()40 machine.train_validate_test(41 train_set_data,train_set_label,42 validate_set_data,validate_set_label,43 test_set_data,test_set_label...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!