Show Attend and Tell的实现代码中的python知识学习
Collection的類Counter()
參考:collections模塊—— Counter
from collections import Counter c = Counter('gallahad') print(c['l']) 輸出:2collections之Counter對象的update方法
參考:collections之Counter對象的update方法
構造一個 Counter對象
執行 update 方法:update([iterable-or-mapping])
""" update 后的參數可以是:可迭代對象 或者 映射 操作原理:如果要更新的關鍵字已存在,則對它的值進行求和;如果不存在,則添加 """ # 1、可迭代對象:字符串 c.update("baaac") c # Out[118]: Counter({'a': 4, 'b': 3, 'c': 4})# 2、映射:字典 c.update({"d":-2, c['a']:-1}) # 因為 a=4, 所以結果中顯示 4: -1 c # Out[128]: Counter({'a': 4, 'b': 3, 'c': 4, 'd': -2, 4: -1})# 3、映射:字典的另一種表現形式 c.update(d=3, a=-1) c # Out[130]: Counter({'a': 3, 'b': 3, 'c': 4, 'd': 1, 4: -1}) from collections import Counter c = Counter('gallahad') print(c) print(c['l']) c.update(['a','v','p','l','k','j']) print(c)輸出
Counter({'a': 3, 'l': 2, 'g': 1, 'h': 1, 'd': 1}) 2 Counter({'a': 4, 'l': 3, 'g': 1, 'h': 1, 'd': 1, 'v': 1, 'p': 1, 'k': 1, 'j': 1})文件:dataset_flickr8k.json的格式
這是一個json文件,里面只有兩個鍵,如下
{"images": [{***},{***},{***},...,{***}], "dataset": "flickr8k"}一共有8000個,這個數是看"imgid"的值的,"imgid"從0到7999,代表每張圖片。
1個{***}里面的內容是
1個{¥¥¥}里面的內容是
{"tokens": ["a", "black", "dog", "is", "running", "after", "a", "white", "dog", "in", "the", "snow"],"raw": "A black dog is running after a white dog in the snow .","imgid": 0,"sentid": 0}, {"tokens": ["black", "dog", "chasing", "brown", "dog", "through", "snow"],"raw": "Black dog chasing brown dog through snow","imgid": 0,"sentid": 1}, {"tokens": ["two", "dogs", "chase", "each", "other", "across", "the", "snowy", "ground"],"raw": "Two dogs chase each other across the snowy ground .","imgid": 0,"sentid": 2}, {"tokens": ["two", "dogs", "play", "together", "in", "the", "snow"],"raw": "Two dogs play together in the snow .","imgid": 0,"sentid": 3}, {"tokens": ["two", "dogs", "running", "through", "a", "low", "lying", "body", "of", "water"],"raw": "Two dogs running through a low lying body of water .","imgid": 0,"sentid": 4}一共有5個,代表每張圖片有5句描述。
python中的enumerate()索引在前,元素在后
words = ['a','v','p','l','k','j'] word_map = {k: v + 1 for v, k in enumerate(words)} print(word_map)for v, k in enumerate(words):print(v,k)輸出
{'a': 1, 'v': 2, 'p': 3, 'l': 4, 'k': 5, 'j': 6} 0 a 1 v 2 p 3 l 4 k 5 jPython-json.dumps()和dump()、json.loads()和load()的區別
參考:Python-json.dumps()和dump()、json.loads()和load()的區別
主要區別:
1、dump(),load() 處理的是json文件
2、dumps(),loads() 處理的是字符串
1、json.dumps()是將字典類型轉化成字符串類型
dic = {'code':0,'msg':'success','agentid':'1000021','state':'fpp_unify_auth'} str = json.dumps(dic) print(type(str)) print(str)運行結果:
<class 'str'> {"code": 0, "msg": "success", "agentid": "1000021", "state": "fpp_unify_auth"}2、json.loads()將字符串類型轉化成字典類型
dic = {'code':0,'msg':'success','agentid':'1000021','state':'fpp_unify_auth'} strs = json.dumps(dic) dics = json.loads(strs) print(type(dics)) print(dics)運行結果:
<class 'dict'> {'code': 0, 'msg': 'success', 'agentid': '1000021', 'state': 'fpp_unify_auth'}3、json.dump()用于將dict類型的數據轉成str,并寫入到json文件中
dic = {'code':0,'msg':'success','agentid':'1000021','state':'fpp_unify_auth'} json_filename = 'test_json.json' json.dump(dic, open(json_filename, "w"))運行結果:
生成test_json.json文件,并寫入內容{"code": 0, "msg": "success", "agentid": "1000021", "state": "fpp_unify_auth"}4、json.load()用于從json文件中讀取數據
json_filename = 'test_json.json' js = json.load(open(json_filename)) print(type(js)) print(js)運行結果:
<class 'dict'> {'code': 0, 'msg': 'success', 'agentid': '1000021', 'state': 'fpp_unify_auth'}for循環
train_image_paths = [] train_image_captions = []val_image_paths = [] val_image_captions = []test_image_paths = [] test_image_captions = []for impaths, imcaps, split in [(train_image_paths, train_image_captions, 'TRAIN'),(val_image_paths, val_image_captions, 'VAL'),(test_image_paths, test_image_captions, 'TEST')]:print(impaths, imcaps, split)輸出結果:
[] [] TRAIN [] [] VAL [] [] TEST這樣輸出的結果解釋:本質還是對列表進行循環,不能被三個變量給嚇住了,每循環列表中的一個元素,這三個變量就會進行相應的對應,這樣就會得到相應的值。
python庫 h5py入門講解
參考:python庫——h5py入門講解
h5py簡單介紹
h5py文件是存放兩類對象的容器,數據集(dataset)和組(group),dataset類似數組類的數據集合,和numpy的數組差不多。group是像文件夾一樣的容器,它好比python中的字典,有鍵(key)和值(value)。group中可以存放dataset或者其他的group?!辨I”就是組成員的名稱,”值”就是組成員對象本身(組或者數據集)
圖片參考:h5py 必知–String存儲
random.choice()
是從序列中獲取一個隨機元素,參考:python:random.choice()方法
import random print(random.choice("I am very help"))輸出結果:
otqdm進度條 在pytorch中的使用
參考:tqdm進度條 在pytorch中的使用
引入該模塊:
tqdm可用于所有可迭代對象,所以可以直接用在dataloader上,如下:
for data, target in tqdm(train_loader): ...效果:
100%|██████████| 1000/1000 [00:27<00:00, 35.91it/s]tqdm用在dataloader上其實是對每個batch和batch總數做的進度條。
采樣
參考:py001- random中sample()函數的用法
sample(list, k)返回一個長度為k新列表,新列表存放list所產生k個隨機唯一的元素
函數用法實例:
輸出:
[1, 2] ['python', 'sky'] [6912, 1869, 5991, 721, 3388]運行
from random import sample imcaps = [["a", "black", "dog", "is", "running", "after", "a", "white", "dog", "in", "the", "snow"],["black", "dog", "chasing", "brown", "dog", "through", "snow"],["two", "dogs", "chase", "each", "other", "across", "the", "snowy", "ground"],["two", "dogs", "play", "together", "in", "the", "snow"],["two", "dogs", "running", "through", "a", "low", "lying", "body", "of", "water"]] captions = sample(imcaps , k=5) print(captions)輸出結果
[['two', 'dogs', 'play', 'together', 'in', 'the', 'snow'], ['a', 'black', 'dog', 'is', 'running', 'after', 'a', 'white', 'dog', 'in', 'the', 'snow'], ['two', 'dogs', 'running', 'through', 'a', 'low', 'lying', 'body', 'of', 'water'], ['black', 'dog', 'chasing', 'brown', 'dog', 'through', 'snow'], ['two', 'dogs', 'chase', 'each', 'other', 'across', 'the', 'snowy', 'ground']]讀取圖片
from imageio import imread img = imread(r'C:\Users\***\Desktop\20210617173058874.png') print(img.shape) print(len(img.shape))輸出
(1134, 1143, 4) 3參考:OpenCV學習筆記(7)圖像的通道(channels)問題
通道數
整理一下OpenCV中文論壇里關于圖像通道的問題,如下:
(1)圖像的通道指的是什么?是不是灰度圖的通道數為1,彩色圖的通道為3?(zhuker)
正確!
基本上,描述一個像素點,如果是灰度,那么只需要一個數值來描述它,就是單通道。
如果一個像素點,有RGB三種顏色來描述它,就是三通道。(ollydbg23)
(2)對于2通道和4通道如何看待?哪位幫忙解釋一下?(feixue)
我見過四通道的,兩通道暫時沒見過,估計只是編程的方便吧。windows的bmp有時候是一個四通道圖像,R、G、B加上一個A通道,表示透明度。(eralvc)
是的,最后這個,一般叫做alpha通道,表示透明度的。(ollydbg23)
4通道通常為RGBA,在某些處理中可能會用到。
2通道圖像不常見,通常在程序處理中會用到,如傅里葉變換,可能會用到,一個通道為實數,一個通道為虛數,主要是編程方便。
還有一種情況就是16位圖像,本來是3通道,但是為了減少數據量,壓縮為16位,剛好兩個通道,常見格式有RGB555或RGB565,也就是說R占5位,G占5或6位,B占5位,也有RGBA5551格式。古老的格式,不用也罷。(Loren)
主要是有些攝像頭常采用一些比較“古怪”的格式,沒辦法。
補充一種情況,目前常見的一些攝像頭喜歡采用YUV2等格式,格式如下YUYV,在處理的時候可以用4通道或者2通道來處理。
如原格式為:Y1UY2V,插值成為Y1UV,Y2UV 就成兩個彩色點了。
YCrCb也有類似壓縮情況。(Loren)
于np.newaxis的一點理解
import numpy as np a = np.arange(0, 10) print(a) print(a.shape) [0 1 2 3 4 5 6 7 8 9] (10,)那么我如何才能將其轉化為shape=(1,10)呢
可以用兩種方法:
1.使用shape
import numpy as np a = np.arange(0, 10) print(a) a.shape=(10,1) print(a) [0 1 2 3 4 5 6 7 8 9] [[0][1][2][3][4][5][6][7][8][9]]2. 使用np.newaxis
import numpy as np a = np.arange(0, 10) print(a) a = a[:,np.newaxis] print(a)結果如下:
[0 1 2 3 4 5 6 7 8 9] [[0][1][2][3][4][5][6][7][8][9]]Numpy中numpy.concatenate()函數
參考:
1、Numpy中numpy.concatenate()函數、numpy.delete()函數、numpy.mean()函數參數axis=0與axis=1的區分
參數axis=0,1,2
2、Python學習筆記——參數axis=0,1,2…
scipy.misc.imresize
參考:
1、python–scipy.misc.imresize()函數
2、scipy筆記—scipy.misc.imresize用法(方便訓練圖像數據)
3、scipy.misc.imresize改變圖像的大小
重新設置圖像像素
Numpy中transpose()函數的可視化理解
參考:Numpy中transpose()函數的可視化理解
cudnn.benchmark = True?是什么意思
參考:cudnn.benchmark = True?是什么意思
總結
以上是生活随笔為你收集整理的Show Attend and Tell的实现代码中的python知识学习的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用onnx包将pth文件转换为onnx
- 下一篇: Python-面向对象的编程语言