{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport os\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport cv2\n\nimport torch\nimport torchvision\n\nfrom torch.utils.data.dataset import Dataset\nfrom torch.utils.data.dataloader import DataLoader\nfrom torchvision import transforms\nfrom PIL import Image\n\nfrom matplotlib.pyplot import figure\n\n\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"12916f21de1419bbc07abcd6f289d7ada1448b14"},"cell_type":"code","source":"fig = plt.figure(figsize=(8, 8), dpi=100,facecolor='w', edgecolor='k')\ntrain_imgs = os.listdir(\"../input/train\")\nfor idx, img in enumerate(np.random.choice(train_imgs, 12)):\n    ax = fig.add_subplot(4, 20//5, idx+1, xticks=[], yticks=[])\n    im = Image.open(\"../input/train/\" + img)\n    plt.imshow(im)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"# load image data\n#image name\ndf = pd.read_csv('../input/train.csv')\ndf.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"60be2a51ffda4b8c215ab329fed3687e0f60c42c"},"cell_type":"code","source":"# lets find total number of different whales present\nprint(f'Training examples: {len(df)}')\nprint(\"Unique whales: \",df['Id'].nunique()) # it includes new_whale as a separate type.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6b25e5f1af478bd37175ed6baf77a21f9d77bef4"},"cell_type":"code","source":"training_pts_per_class = df.groupby('Id').size()\nprint(training_pts_per_class)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a6f0d2cb498657ba1d701a745305d9da42f32b65"},"cell_type":"code","source":"print(\"Min example a class can have: \",training_pts_per_class.min())\nprint(\"0.99 quantile: \",training_pts_per_class.quantile(0.99))\nprint(\"Max example a class can have: \\n\",training_pts_per_class.nlargest(2))    \n# max value belongs to new_whale category so the second max is the appropriate \n# representation of the max data points for a particualar class.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2baf9238cc2513909e704b2929a569f3d8ee94b0"},"cell_type":"code","source":"data = training_pts_per_class.copy()\ndata.loc[data > data.quantile(0.99)] = '22+'\nplt.figure(figsize=(15,10))\nsns.countplot(data.astype('str'))\nplt.title(\"#classes with different number of images\",fontsize=15)\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"032c598c5569f28dffcacf153b7cbfc8d3843e5e"},"cell_type":"code","source":"# new_whales is addes as a new class.\n# above graph shows that there are more than 2000 classes with just one training example.\n# and around 1300 classes with 2 training examples.\n# it also shows that around 50 classes have more than 22 training examples.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1e97ec24201fd445afbdf68e9a64d780b04e5b8c"},"cell_type":"code","source":"print(data)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"dc36a6cd63a006664082b0b381cec14e1ed98c15"},"cell_type":"code","source":"print(len(os.listdir('../input/train/')))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"836f9bad3c680e44ac26da13df3e0292a2aa7085"},"cell_type":"code","source":"class HW_Dataset(Dataset):\n    def __init__(self,filepath, csv_path,transform=None):\n        self.file_path = filepath\n        self.df = pd.read_csv(csv_path)\n        self.transform = transform\n        self.image_list = [x for x in os.listdir(self.file_path)]\n        \n    def __len__(self):\n        return(len(self.image_list))\n    \n    def __getitem__(self,idx):\n        img_path = os.path.join(self.file_path,self.df.Image[idx])\n        label = self.df.Id[idx]\n        \n        #img = cv2.imread(img_path)\n        #img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n        img = Image.open(img_path).convert('RGB')\n        img = self.transform(img)\n        \n        return img, label\n        ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"991963a17b8e1ad633d642c3f6b1f79762bd37ca"},"cell_type":"code","source":"transform = transforms.Compose([transforms.Resize((256,256)),\n                                transforms.ToTensor()])\n                                #transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) ])\n\ntrain_dataset = HW_Dataset('../input/train/','../input/train.csv', transform)\ndata_generator = DataLoader(train_dataset,batch_size=16, shuffle=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"fefb70c0a3bf1d3bce5ae0d97de15f817db9d687"},"cell_type":"code","source":"images, labels = next(iter(data_generator))\nprint(f'images size: {images.size()}')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e33c24b274596bd5cac50da99fafba3d32599a8a"},"cell_type":"code","source":"figure(num=None, figsize=(8, 8), dpi=100, facecolor='w', edgecolor='k')\ngrid = torchvision.utils.make_grid(images,nrow=4)\nplt.imshow(grid.numpy().transpose((1,2,0)))\nplt.axis('off')\n#plt.title(labels)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"36fe5fe695d8990db25e9d5f7ca4152364ffccc7"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}