{"id":93,"date":"2023-03-10T07:29:41","date_gmt":"2023-03-10T07:29:41","guid":{"rendered":"https:\/\/smartsource.com.sg\/blog\/?p=93"},"modified":"2023-03-10T07:29:41","modified_gmt":"2023-03-10T07:29:41","slug":"generate-sample-image-dataset-using-pytorch","status":"publish","type":"post","link":"https:\/\/smartsource.com.sg\/blog\/index.php\/2023\/03\/10\/generate-sample-image-dataset-using-pytorch\/","title":{"rendered":"Generate sample image dataset using PyTorch"},"content":{"rendered":"\n<p>Here is some sample code using PyTorch for generating a sample dataset using the MNIST dataset as an example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import torch\r\nimport torchvision\r\nfrom torchvision import transforms\r\n\r\n# define the transformations to be applied to the data\r\ntransform = transforms.Compose(&#91;\r\n    transforms.ToTensor(),\r\n    transforms.Normalize((0.5,), (0.5,))\r\n])\r\n\r\n# load the MNIST dataset\r\ntrainset = torchvision.datasets.MNIST(root='.\/data', train=True, download=True, transform=transform)\r\ntrainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True)\r\n\r\n# define a function to generate new samples\r\ndef generate_samples(num_samples):\r\n    samples = &#91;]\r\n    for i in range(num_samples):\r\n        # randomly sample a batch of images from the dataset\r\n        images, labels = iter(trainloader).next()\r\n        # flatten the images\r\n        images = images.view(images.size(0), -1)\r\n        # add the images to the list of samples\r\n        samples.append(images)\r\n    # concatenate the samples into a single tensor\r\n    samples = torch.cat(samples, dim=0)\r\n    return samples\r\n\r\n# generate 100 new samples\r\nnew_samples = generate_samples(100)\r\nprint(new_samples.shape)\r<\/code><\/pre>\n\n\n\n<p>In this code, we first define the transformations to be applied to the data, which includes converting the data to a PyTorch tensor and normalizing the pixel values. We then load the MNIST dataset and define a PyTorch DataLoader to sample batches of data from the dataset.<\/p>\n\n\n\n<p>We then define a function <code>generate_samples<\/code> that takes in a number of samples to generate and randomly samples batches of data from the DataLoader. We flatten the images and add them to a list of samples, which we concatenate into a single tensor and return.<\/p>\n\n\n\n<p>Finally, we generate 100 new samples using the <code>generate_samples<\/code> function and print the shape of the resulting tensor.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is some sample code using PyTorch for generating a sample dataset using the MNIST dataset as an example: In this code, we first define the transformations to be applied&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[68,69,77,76],"class_list":["post-93","post","type-post","status-publish","format-standard","hentry","category-tutorials","tag-dataset","tag-generate","tag-mnist","tag-pytorch"],"_links":{"self":[{"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/posts\/93","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=93"}],"version-history":[{"count":1,"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/posts\/93\/revisions"}],"predecessor-version":[{"id":94,"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/posts\/93\/revisions\/94"}],"wp:attachment":[{"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=93"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=93"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/smartsource.com.sg\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=93"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}