Pytorch: Prepare, train and test your own picture data

Source: Internet
Author: User
Tags gz file prepare zip pytorch dataloader

Most of the Pytorch introductory tutorials are trained and tested using the data in the torchvision. If we are our own picture data, how to do it.

one, my data

When I was studying, I used the fashion-mnist. This data is relatively small, my computer does not have the GPU, but also can feel all. About Fashion-mnist data, can Baidu, also can point this to understand, the data is like this look:

Download Address: Https://github.com/zalandoresearch/fashion-mnist

But the download is a binary file, not a picture, so I first converted into a picture.

I first unzip the GZ file to the e:/fashion_mnist/folder

Then run the code:

Import OS from skimage import IO import torchvision.datasets.mnist as mnist root= "e:/fashion_mnist/" Train_set = (mn Ist.read_image_file (Os.path.join (Root, ' train-images-idx3-ubyte ')), Mnist.read_label_file (Os.path.join (Root, ' 
    Train-labels-idx1-ubyte ')) Test_set = (Mnist.read_image_file (os.path.join (Root, ' t10k-images-idx3-ubyte ')), Mnist.read_label_file (Os.path.join (Root, ' t10k-labels-idx1-ubyte ')) print ("Training set:", Train_set[0].siz E ()) print ("Test set:", Test_set[0].size ()) def convert_to_img (train=true): if (train): F=open (root+ ' Train.txt
        ', ' W ') data_path=root+ '/train/' if (not os.path.exists (Data_path)): Os.makedirs (Data_path)
            For I, (Img,label) in Enumerate (Zip (train_set[0],train_set[1])): Img_path=data_path+str (i) + '. jpg '
        Io.imsave (Img_path,img.numpy ()) f.write (img_path+ ' +str (label) + ' \ n ') F.close () Else: F = open (root + ' test.txt ', ' W ')
        Data_path = root + '/test/' if (not os.path.exists (Data_path)): Os.makedirs (Data_path)
            For I, (Img,label) in Enumerate (Zip (test_set[0],test_set[1])): Img_path = data_path+ Str (i) + '. jpg ' Io.imsave (Img_path, Img.numpy ()) F.write (Img_path + "+ str (label) + ' \ n ') f.close () convert_t O_img (True) convert_to_img (False)

This will generate the train and test folders, respectively, in the e:/fashion_mnist/directory for storing images. The label files Train.txt and Test.txt are also generated under this directory.

second, the CNN classification training and Testing

First to read out the picture, ready to torch dedicated dataset format, and then through the Dataloader batch training.

The code is as follows:

Import Torch from Torch.autograd import Variable from torchvision Import transforms from torch.utils.data import Dataset, Dataloader from PIL import Image root= "e:/fashion_mnist/" #-----------------Ready The DataSet--------------------------def default_loader (path): Return Image.open (Path). Convert (' RGB ') class myDataSet ( Dataset): Def __init__ (self, txt, transform=none, Target_transform=none, loader=default_loader): FH = open (txt
            , ' r ') IMGs = [] for line in fh:line = Line.strip (' \ n ') line = Line.rstrip () Words = Line.split () imgs.append ((Words[0],int (words[1))) Self.imgs = IMGs Self.trans Form = Transform Self.target_transform = target_transform Self.loader = Loader def __getitem__ (self,
            Index): FN, label = Self.imgs[index] img = Self.loader (FN) if Self.transform is not None: img = Self.transform (IMG) return Img,label DEF __len__ (self): return len (Self.imgs) train_data=mydataset (txt=root+ ' train.txt ', transform=transforms. Totensor ()) Test_data=mydataset (txt=root+ ' test.txt ', transform=transforms. Totensor ()) Train_loader = Dataloader (Dataset=train_data, batch_size=64, shuffle=true) Test_loader = DataLoader ( Dataset=test_data, batch_size=64) #-----------------Create the net and training------------------------class Net (Torc
            H.nn.module): def __init__ (self): Super (Net, self). __init__ () Self.conv1 = Torch.nn.Sequential ( Torch.nn.Conv2d (3, 3, 1, 1), Torch.nn.ReLU (), torch.nn.MaxPool2d (2)) Self.conv2 = Torch.nn.Sequential (torch.nn.Conv2d (3, 1, 1), Torch.nn.ReLU (), Torch.nn.Max POOL2D (2)) Self.conv3 = Torch.nn.Sequential (torch.nn.Conv2d (3, 1, 1), to Rch.nn.ReLU (), torch.nn.MaxPool2d (2)) Self.dense = Torch.nn.SeqUential (Torch.nn.Linear (3 * 3, +), Torch.nn.ReLU (), Torch.nn.Linear (128, 10) ) def forward (self, x): Conv1_out = SELF.CONV1 (x) conv2_out = Self.conv2 (conv1_out) CO
        Nv3_out = Self.conv3 (conv2_out) res = Conv3_out.view (conv3_out.size (0),-1) out = Self.dense (res) Return out model = Net () print (model) optimizer = Torch.optim.Adam (Model.parameters ()) Loss_func = Torch.nn.CrossEntrop Yloss () for epoch in range: print (' Epoch {} '. Format (epoch + 1)) # training-----------------------------T
    Rain_loss = 0.
    TRAIN_ACC = 0. For batch_x, batch_y in train_loader:batch_x, batch_y = Variable (batch_x), Variable (batch_y) out = Model (
        batch_x) loss = Loss_func (out, batch_y) Train_loss + = loss.data[0] pred = Torch.max (out, 1) [1] Train_correct = (pred = = batch_y). SUM () Train_acc + = train_correct.data[0] Optimizer.zeRo_grad () Loss.backward () Optimizer.step () print (' Train loss: {:. 6f}, ACC: {:. 6f} '. Format (Train_loss/ (Len (Train_data)), TRAIN_ACC/(Len (Train_data))) # Evaluation--------------------------------MODEL.E
    Val () Eval_loss = 0.
    EVAL_ACC = 0. For batch_x, batch_y in test_loader:batch_x, batch_y = Variable (batch_x, Volatile=true), Variable (Batch_y, Volati Le=true) out = Model (batch_x) loss = Loss_func (out, batch_y) Eval_loss + = Loss.data[0] Pre D = Torch.max (out, 1) [1] num_correct = (pred = = batch_y). SUM () Eval_acc + = num_correct.data[0] Print (' Test Loss: {:. 6f}, ACC: {:. 6f} '. Format (Eval_loss/(Len (Test_data)), EVAL_ACC/(Len (test_data)))

Print out the network model:

Training and test results:

Original link

Https://www.cnblogs.com/denny402/p/7520063.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.