CIFAR10及cifar100 dataset 的下載與使用、轉圖片

 

【時間】2019.12.21

【題目】CIFAR-10 dataset 的下載與使用、轉圖片

cifar10與cifar100下載地址:http://www.cs.toronto.edu/~kriz/cifar.html

一、cifar10介紹及轉換圖片

具體參考:CIFAR-10 dataset 的下載與使用、轉圖片.

解壓源文件的函數:

import cv2
import numpy as np
import pickle
import os

# 解壓縮,返回解壓後的字典
def unpickle(file):
    fo = open(file, 'rb')
    dict = pickle.load(fo, encoding='latin1')
    fo.close()
    return dict

cifar10轉圖片 :

def cifar10_to_images():
  tar_dir='../data/cifar-10-batches-py/' #原始數據庫目錄
  train_root_dir='../data/cifar10/train/' #圖片保存目錄
  test_root_dir='../data/cifar10/test/'
  if not os.path.exists(train_root_dir):
    os.makedirs(train_root_dir)
  if not os.path.exists(test_root_dir):
    os.makedirs(test_root_dir)
  # 生成訓練集圖片,如果需要png格式,只需要改圖片後綴名即可。
  label_names = ["airplane", "automobile", "bird", "cat", "dear", "dog", "frog", "horse", "ship", "truck"]
  for j in range(1, 6):
      dataName = tar_dir+"data_batch_" + str(j) 
      Xtr = unpickle(dataName)
      print(dataName + " is loading...")

      for i in range(0, 10000):
          img = np.reshape(Xtr['data'][i], (3, 32, 32))  # Xtr['data']爲圖片二進制數據
          img = img.transpose(1, 2, 0)  # 讀取image
          picName = train_root_dir + str(Xtr['labels'][i])+ '_' + label_names[Xtr['labels'][i]]+'_'+ str(i + (j - 1)*10000)+'.jpg'  # label+class+index
          cv2.imwrite(picName, img)
      print(dataName + " loaded.")

  print("test_batch is loading...")

  # 生成測試集圖片
  testXtr = unpickle(tar_dir+"test_batch")
  for i in range(0, 10000):
      img = np.reshape(testXtr['data'][i], (3, 32, 32))
      img = img.transpose(1, 2, 0)
      picName = test_root_dir + str(testXtr['labels'][i])+ '_' + label_names[testXtr['labels'][i]]+'_' + str(i) + '.jpg'
      cv2.imwrite(picName, img)
  print("test_batch loaded.")

結果:文件名爲 # label_class_index.jpg

 

二、cifar100介紹及轉換圖片

cifar100包含20個大類,共100類,train集50000張圖片,test集10000張圖片。

 cifar100轉圖片 :

def cifar100_to_images():
  tar_dir='../data/cifar-100-python/' #原始數據庫目錄
  train_root_dir='../data/cifar100/train/' #圖片保存目錄
  test_root_dir='../data/cifar100/test/'
  if not os.path.exists(train_root_dir):
    os.makedirs(train_root_dir)
  if not os.path.exists(test_root_dir):
    os.makedirs(test_root_dir)

  #獲取label對應的class,分爲20個coarse class,共100個 fine class
  meta_Name = tar_dir+"meta" 
  Meta_dic= unpickle(meta_Name)
  coarse_label_names=Meta_dic['coarse_label_names']
  fine_label_names=Meta_dic['fine_label_names']
  print(fine_label_names)

  #生成訓練集圖片,如果需要png格式,只需要改圖片後綴名即可。
  dataName = tar_dir+"train" 
  Xtr = unpickle(dataName)
  print(dataName + " is loading...")
  for i in range(0,Xtr['data'].shape[0]):
      img = np.reshape(Xtr['data'][i], (3, 32, 32))  # Xtr['data']爲圖片二進制數據
      img = img.transpose(1, 2, 0)  # 讀取image
      ###img_name:fine_label+coarse_label+fine_class+coarse_class+index
      picName = train_root_dir + str(Xtr['fine_labels'][i])+ '_' + str(Xtr['coarse_labels'][i]) + '_&' +fine_label_names[Xtr['fine_labels'][i]]+'&_'+coarse_label_names[ Xtr['coarse_labels'][i]]+'_'+str(i) + '.jpg' 
      cv2.imwrite(picName, img)
  print(dataName + " loaded.")

  print("test_batch is loading...")
  # 生成測試集圖片
  testXtr = unpickle(tar_dir+"test")
  for i in range(0, testXtr['data'].shape[0]):
      img = np.reshape(testXtr['data'][i], (3, 32, 32))
      img = img.transpose(1, 2, 0)
      picName = test_root_dir +str(testXtr['fine_labels'][i])+ '_' + str(testXtr['coarse_labels'][i]) + '_&' +fine_label_names[testXtr['fine_labels'][i]]+'&_'+coarse_label_names[ testXtr['coarse_labels'][i]]+'_'+str(i) + '.jpg' 
      cv2.imwrite(picName, img)
  print("test_batch loaded.")

結果:文件名爲 # fine_label+coarse_label+fine_class+coarse_class+index.jpg

fine表示類,coarse表示大類。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章