類中的__init__()和__call__()函式
目錄
自己認為的:
在下面的程式碼中,用到了類,其中類中的__init__(self, output_size)和__call__(self, output_size)函式,介紹一下。
當我一遍一遍的debug的時候,scale = Rescale(256)中的256賦值output_size,進行的是初始化。而沒有呼叫__call__()。
scale = Rescale(256) crop = RandomCrop(128)
在下面程式碼的時候,採用enumerate來返回的sample_batched(等價於Rescale和RandomCrop的類例項)來呼叫__call__()
for i_batch, sample_batched in enumerate(dataloader): print(i_batch, sample_batched['image'].size(), sample_batched['landmarks'].size())
所以一個類例項也可以成為類似函式這樣能直接呼叫的物件,只要定義的時候有__call__()方法就可以。
參考別人的:
連結:https://www.cnblogs.com/lyu454978790/p/8630215.html
具體看下這裡:
>>>class Reader():
def __init__(self,name,nationality):
self.name = name
self.nationality = nationality
def __call__(self):
print('Reader: %s Nationality: %s' % (self.name, self.nationality))
>>>r = Reader('Annie','Chinese')
>>>r()
Reader:Annie Nationality: Chinese
__call__()方法還可以帶引數
定義一個可以直接呼叫類例項的Reader類,並可統計讀者數量
>>>class Reader():
count = 0
def __init__(self,name,nationality):
self.name = name
self.nationality = nationality
Reader.count += 1
def __call__(self, behave):
print('Reader: %s' % self.name)
print('Nationality: %s' % self.nationality)
print('%s is being %s.' % (self.name, behave))
print('The total number of readers is %s.' % Reader.count)
>>>a = Reader('Annie','Chinese')
>>>a('Nice')
Reader: Annie
Nationality: Chinese
Annie is being Nice.
The total number of readers is 1.
>>>b = Reader('Adam','American')
>>>b('Silly')
Reader: Adam
Nationality: American
Adam is being Silly.
The total number of readers is 2. #自動增加
程式碼連結:https://pytorch.apachecn.org/docs/1.4/5.html
對應於(自己認為的)那部分的程式碼
main函式
from __future__ import print_function, division
import os
import torch
import pandas as pd
from skimage import io, transform
import numpy as np
import matplotlib.pyplot as plt
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms, utils
from Rescale import Rescale,RandomCrop,ToTensor
# Ignore warnings
import warnings
from FaceLandmarksDataset import FaceLandmarksDataset
warnings.filterwarnings("ignore")
plt.ion() # interactive mode
landmarks_frame = pd.read_csv('./data/faces/face_landmarks.csv')
n = 65
img_name = landmarks_frame.iloc[n, 0]
landmarks = landmarks_frame.iloc[n, 1:]
landmarks = np.asarray(landmarks)
landmarks = landmarks.astype('float').reshape(-1, 2)
print('Image name: {}'.format(img_name))
print('Landmarks shape: {}'.format(landmarks.shape))
print('First 4 Landmarks: {}'.format(landmarks[:4]))
def show_landmarks(image, landmarks):
"""Show image with landmarks"""
plt.imshow(image)
plt.scatter(landmarks[:, 0], landmarks[:, 1], s=10, marker='.', c='r')
plt.pause(0.001) # pause a bit so that plots are updated
plt.figure()
show_landmarks(io.imread(os.path.join('data/faces/', img_name)),
landmarks)
plt.show()
face_dataset = FaceLandmarksDataset(csv_file='data/faces/face_landmarks.csv',
root_dir='data/faces/')
fig = plt.figure()
for i in range(len(face_dataset)):
sample = face_dataset[i]
print(i, sample['image'].shape, sample['landmarks'].shape)
ax = plt.subplot(1, 4, i + 1)
plt.tight_layout()
ax.set_title('Sample #{}'.format(i))
ax.axis('off')
show_landmarks(**sample)
if i == 3:
plt.show()
break
scale = Rescale(256)
crop = RandomCrop(128)
composed = transforms.Compose([Rescale(256),
RandomCrop(224)])
# Apply each of the above transforms on sample.
fig = plt.figure()
sample = face_dataset[65]
for i, tsfrm in enumerate([scale, crop, composed]):
transformed_sample = tsfrm(sample)
ax = plt.subplot(1, 3, i + 1)
plt.tight_layout()
ax.set_title(type(tsfrm).__name__)
show_landmarks(**transformed_sample)
plt.show()
transformed_dataset = FaceLandmarksDataset(csv_file='data/faces/face_landmarks.csv',
root_dir='data/faces/',
transform=transforms.Compose([
Rescale(256),
RandomCrop(224),
ToTensor()
]))
for i in range(len(transformed_dataset)):
sample = transformed_dataset[i]
print(i, sample['image'].size(), sample['landmarks'].size())
if i == 3:
break
dataloader = DataLoader(transformed_dataset, batch_size=4,
shuffle=True, num_workers=4)
# Helper function to show a batch
def show_landmarks_batch(sample_batched):
"""Show image with landmarks for a batch of samples."""
images_batch, landmarks_batch = \
sample_batched['image'], sample_batched['landmarks']
batch_size = len(images_batch)
im_size = images_batch.size(2)
grid_border_size = 2
grid = utils.make_grid(images_batch)
plt.imshow(grid.numpy().transpose((1, 2, 0)))
for i in range(batch_size):
plt.scatter(landmarks_batch[i, :, 0].numpy() + i * im_size + (i + 1) * grid_border_size,
landmarks_batch[i, :, 1].numpy() + grid_border_size,
s=10, marker='.', c='r')
plt.title('Batch from dataloader')
for i_batch, sample_batched in enumerate(dataloader):
print(i_batch, sample_batched['image'].size(),
sample_batched['landmarks'].size())
# observe 4th batch and stop.
if i_batch == 3:
plt.figure()
show_landmarks_batch(sample_batched)
plt.axis('off')
plt.ioff()
plt.show()
break
類函式
import torch
from skimage import io, transform
import numpy as np
class Rescale(object):
"""Rescale the image in a sample to a given size.
Args:
output_size (tuple or int): Desired output size. If tuple, output is
matched to output_size. If int, smaller of image edges is matched
to output_size keeping aspect ratio the same.
"""
def __init__(self, output_size):
assert isinstance(output_size, (int, tuple))
self.output_size = output_size
def __call__(self, sample):
image, landmarks = sample['image'], sample['landmarks']
h, w = image.shape[:2]
if isinstance(self.output_size, int):
if h > w:
new_h, new_w = self.output_size * h / w, self.output_size
else:
new_h, new_w = self.output_size, self.output_size * w / h
else:
new_h, new_w = self.output_size
new_h, new_w = int(new_h), int(new_w)
img = transform.resize(image, (new_h, new_w))
# h and w are swapped for landmarks because for images,
# x and y axes are axis 1 and 0 respectively
landmarks = landmarks * [new_w / w, new_h / h]
return {'image': img, 'landmarks': landmarks}
class RandomCrop(object):
"""Crop randomly the image in a sample.
Args:
output_size (tuple or int): Desired output size. If int, square crop
is made.
"""
def __init__(self, output_size):
assert isinstance(output_size, (int, tuple))
if isinstance(output_size, int):
self.output_size = (output_size, output_size)
else:
assert len(output_size) == 2
self.output_size = output_size
def __call__(self, sample):
image, landmarks = sample['image'], sample['landmarks']
h, w = image.shape[:2]
new_h, new_w = self.output_size
top = np.random.randint(0, h - new_h)
left = np.random.randint(0, w - new_w)
image = image[top: top + new_h,
left: left + new_w]
landmarks = landmarks - [left, top]
return {'image': image, 'landmarks': landmarks}
class ToTensor(object):
"""Convert ndarrays in sample to Tensors."""
def __call__(self, sample):
image, landmarks = sample['image'], sample['landmarks']
# swap color axis because
# numpy image: H x W x C
# torch image: C X H X W
image = image.transpose((2, 0, 1))
return {'image': torch.from_numpy(image),
'landmarks': torch.from_numpy(landmarks)}
相關文章
- 簡述Python類中的 __init__、__new__、__call__ 方法Python
- python中的“__init__”函式Python函式
- python中的__init__ 、__new__、__call__小結Python
- Python中的__new__、__init__、__call__三個特殊方法Python
- python類中super()和__init__()的區別Python
- 如何使用函式指標呼叫類中的函式和普通函式函式指標
- Python 中__init__函式以及引數selfPython函式
- 類函式和物件函式 PHP函式物件PHP
- Python學習系列之類的定義、建構函式 def __init__Python函式
- python2中的__new__與__init__,新式類和經典類Python
- C++中抽象類、虛擬函式和純虛擬函式C++抽象函式
- Rust中的into函式和from函式Rust函式
- 子父類中函式函式
- 函式模版和類模版函式
- python中的__call__Python
- Python中__init__的用法和理解Python
- JavaScript中的compose函式和pipe函式JavaScript函式
- 轉:類似SQL中的split函式SQL函式
- CUDA函式的概念、種類和示例函式
- Python 中的 super(類名, self).__init__() 的含義Python
- php中呼叫類的屬性和函式的方法->_=>_::_$this->區別PHP函式
- python中calss(類)的使用,類的教程,類中的函式怎麼呼叫。Python函式
- React函式式元件和類元件[Dan]React函式元件
- 父類和子類的建構函式問題函式
- React 是如何分辨函式式元件和類元件的?React函式元件
- Processing中PImage類和loadImage()、createImage()函式的相關解析函式
- 友元函式和友元類函式
- python中 __call__ 的應用Python
- 類的建構函式和解構函式函式
- JS 中的函式表示式和函式宣告你混淆了嗎?JS函式
- python中__init__ 和__new__的對比Python
- 詳解Python中的__init__和__new__Python
- oracle中的chr()和ascii()函式OracleASCII函式
- 詳解Python魔法函式,__init__,__str__,__del__Python函式
- 前端常用的工具類函式, 持續更新中前端函式
- dart系列之:dart類中的建構函式Dart函式
- Oracle 中 replace函式和translate函式比較Oracle函式
- JavaScript 中函式節流和函式去抖的講解JavaScript函式