最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

keras-siamese用自己的數(shù)據(jù)集實現(xiàn)詳解

 更新時間:2020年06月10日 09:15:29   作者:莫離已成歌  
這篇文章主要介紹了keras-siamese用自己的數(shù)據(jù)集實現(xiàn)詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

Siamese網(wǎng)絡(luò)不做過多介紹,思想并不難,輸入兩個圖像,輸出這兩張圖像的相似度,兩個輸入的網(wǎng)絡(luò)結(jié)構(gòu)是相同的,參數(shù)共享。

主要發(fā)現(xiàn)很多代碼都是基于mnist數(shù)據(jù)集的,下面說一下怎么用自己的數(shù)據(jù)集實現(xiàn)siamese網(wǎng)絡(luò)。

首先,先整理數(shù)據(jù)集,相同的類放到同一個文件夾下,如下圖所示:

接下來,將pairs及對應(yīng)的label寫到csv中,代碼如下:

import os
import random
import csv
#圖片所在的路徑
path = '/Users/mac/Desktop/wxd/flag/category/'
#files列表保存所有類別的路徑
files=[]
same_pairs=[]
different_pairs=[]
for file in os.listdir(path):
 if file[0]=='.':
  continue
 file_path = os.path.join(path,file)
 files.append(file_path)
#該地址為csv要保存到的路徑,a表示追加寫入
with open('/Users/mac/Desktop/wxd/flag/data.csv','a') as f:
 #保存相同對
 writer = csv.writer(f)
 for file in files:
  imgs = os.listdir(file) 
  for i in range(0,len(imgs)-1):
   for j in range(i+1,len(imgs)):
    pairs = []
    name = file.split(sep='/')[-1]
    pairs.append(path+name+'/'+imgs[i])
    pairs.append(path+name+'/'+imgs[j])
    pairs.append(1)
    writer.writerow(pairs)
 #保存不同對
 for i in range(0,len(files)-1):
  for j in range(i+1,len(files)):
   filea = files[i]
   fileb = files[j]
   imga_li = os.listdir(filea)
   imgb_li = os.listdir(fileb)
   random.shuffle(imga_li)
   random.shuffle(imgb_li)
   a_li = imga_li[:]
   b_li = imgb_li[:]
   for p in range(len(a_li)):
    for q in range(len(b_li)):
     pairs = []
     name1 = filea.split(sep='/')[-1]
     name2 = fileb.split(sep='/')[-1]
     pairs.append(path+name1+'/'+a_li[p])
     pairs.append(path+name2+'/'+b_li[q])
     pairs.append(0)
     writer.writerow(pairs)

相當(dāng)于csv每一行都包含一對結(jié)果,每一行有三列,第一列第一張圖片路徑,第二列第二張圖片路徑,第三列是不是相同的label,屬于同一個類的label為1,不同類的為0,可參考下圖:

然后,由于keras的fit函數(shù)需要將訓(xùn)練數(shù)據(jù)都塞入內(nèi)存,而大部分訓(xùn)練數(shù)據(jù)都較大,因此才用fit_generator生成器的方法,便可以訓(xùn)練大數(shù)據(jù),代碼如下:

from __future__ import absolute_import
from __future__ import print_function
import numpy as np
from keras.models import Model
from keras.layers import Input, Dense, Dropout, BatchNormalization, Conv2D, MaxPooling2D, AveragePooling2D, concatenate, \
 Activation, ZeroPadding2D
from keras.layers import add, Flatten
from keras.utils import plot_model
from keras.metrics import top_k_categorical_accuracy
from keras.preprocessing.image import ImageDataGenerator
from keras.models import load_model
import tensorflow as tf
import random
import os
import cv2
import csv
import numpy as np
from keras.models import Model
from keras.layers import Input, Flatten, Dense, Dropout, Lambda
from keras.optimizers import RMSprop
from keras import backend as K
from keras.callbacks import ModelCheckpoint
from keras.preprocessing.image import img_to_array
 
"""
自定義的參數(shù)
"""
im_width = 224
im_height = 224
epochs = 100
batch_size = 64
iterations = 1000
csv_path = ''
model_result = ''
 
 
# 計算歐式距離
def euclidean_distance(vects):
 x, y = vects
 sum_square = K.sum(K.square(x - y), axis=1, keepdims=True)
 return K.sqrt(K.maximum(sum_square, K.epsilon()))
 
def eucl_dist_output_shape(shapes):
 shape1, shape2 = shapes
 return (shape1[0], 1)
 
# 計算loss
def contrastive_loss(y_true, y_pred):
 '''Contrastive loss from Hadsell-et-al.'06
 http://yann.lecun.com/exdb/publis/pdf/hadsell-chopra-lecun-06.pdf
 '''
 margin = 1
 square_pred = K.square(y_pred)
 margin_square = K.square(K.maximum(margin - y_pred, 0))
 return K.mean(y_true * square_pred + (1 - y_true) * margin_square)
 
def compute_accuracy(y_true, y_pred):
 '''計算準(zhǔn)確率
 '''
 pred = y_pred.ravel() < 0.5
 print('pred:', pred)
 return np.mean(pred == y_true)
 
def accuracy(y_true, y_pred):
 '''Compute classification accuracy with a fixed threshold on distances.
 '''
 return K.mean(K.equal(y_true, K.cast(y_pred < 0.5, y_true.dtype)))
 
def processImg(filename):
 """
 :param filename: 圖像的路徑
 :return: 返回的是歸一化矩陣
 """
 img = cv2.imread(filename)
 img = cv2.resize(img, (im_width, im_height))
 img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
 img = img_to_array(img)
 img /= 255
 return img
 
def Conv2d_BN(x, nb_filter, kernel_size, strides=(1, 1), padding='same', name=None):
 if name is not None:
  bn_name = name + '_bn'
  conv_name = name + '_conv'
 else:
  bn_name = None
  conv_name = None
 
 x = Conv2D(nb_filter, kernel_size, padding=padding, strides=strides, activation='relu', name=conv_name)(x)
 x = BatchNormalization(axis=3, name=bn_name)(x)
 return x
 
def bottleneck_Block(inpt, nb_filters, strides=(1, 1), with_conv_shortcut=False):
 k1, k2, k3 = nb_filters
 x = Conv2d_BN(inpt, nb_filter=k1, kernel_size=1, strides=strides, padding='same')
 x = Conv2d_BN(x, nb_filter=k2, kernel_size=3, padding='same')
 x = Conv2d_BN(x, nb_filter=k3, kernel_size=1, padding='same')
 if with_conv_shortcut:
  shortcut = Conv2d_BN(inpt, nb_filter=k3, strides=strides, kernel_size=1)
  x = add([x, shortcut])
  return x
 else:
  x = add([x, inpt])
  return x
 
def resnet_50():
 width = im_width
 height = im_height
 channel = 3
 inpt = Input(shape=(width, height, channel))
 x = ZeroPadding2D((3, 3))(inpt)
 x = Conv2d_BN(x, nb_filter=64, kernel_size=(7, 7), strides=(2, 2), padding='valid')
 x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='same')(x)
 
 # conv2_x
 x = bottleneck_Block(x, nb_filters=[64, 64, 256], strides=(1, 1), with_conv_shortcut=True)
 x = bottleneck_Block(x, nb_filters=[64, 64, 256])
 x = bottleneck_Block(x, nb_filters=[64, 64, 256])
 
 # conv3_x
 x = bottleneck_Block(x, nb_filters=[128, 128, 512], strides=(2, 2), with_conv_shortcut=True)
 x = bottleneck_Block(x, nb_filters=[128, 128, 512])
 x = bottleneck_Block(x, nb_filters=[128, 128, 512])
 x = bottleneck_Block(x, nb_filters=[128, 128, 512])
 
 # conv4_x
 x = bottleneck_Block(x, nb_filters=[256, 256, 1024], strides=(2, 2), with_conv_shortcut=True)
 x = bottleneck_Block(x, nb_filters=[256, 256, 1024])
 x = bottleneck_Block(x, nb_filters=[256, 256, 1024])
 x = bottleneck_Block(x, nb_filters=[256, 256, 1024])
 x = bottleneck_Block(x, nb_filters=[256, 256, 1024])
 x = bottleneck_Block(x, nb_filters=[256, 256, 1024])
 
 # conv5_x
 x = bottleneck_Block(x, nb_filters=[512, 512, 2048], strides=(2, 2), with_conv_shortcut=True)
 x = bottleneck_Block(x, nb_filters=[512, 512, 2048])
 x = bottleneck_Block(x, nb_filters=[512, 512, 2048])
 
 x = AveragePooling2D(pool_size=(7, 7))(x)
 x = Flatten()(x)
 x = Dense(128, activation='relu')(x)
 return Model(inpt, x)
 
def generator(imgs, batch_size):
 """
 自定義迭代器
 :param imgs: 列表,每個包含一對矩陣以及l(fā)abel
 :param batch_size:
 :return:
 """
 while 1:
  random.shuffle(imgs)
  li = imgs[:batch_size]
  pairs = []
  labels = []
  for i in li:
   img1 = i[0]
   img2 = i[1]
   im1 = cv2.imread(img1)
   im2 = cv2.imread(img2)
   if im1 is None or im2 is None:
    continue
   label = int(i[2])
   img1 = processImg(img1)
   img2 = processImg(img2)
   pairs.append([img1, img2])
   labels.append(label)
  pairs = np.array(pairs)
  labels = np.array(labels)
  yield [pairs[:, 0], pairs[:, 1]], labels
 
input_shape = (im_width, im_height, 3)
base_network = resnet_50()
 
input_a = Input(shape=input_shape)
input_b = Input(shape=input_shape)
 
# because we re-use the same instance `base_network`,
# the weights of the network
# will be shared across the two branches
processed_a = base_network(input_a)
processed_b = base_network(input_b)
 
distance = Lambda(euclidean_distance,
     output_shape=eucl_dist_output_shape)([processed_a, processed_b])
with tf.device("/gpu:0"):
 model = Model([input_a, input_b], distance)
 # train
 rms = RMSprop()
 rows = csv.reader(open(csv_path, 'r'), delimiter=',')
 imgs = list(rows)
 checkpoint = ModelCheckpoint(filepath=model_result+'flag_{epoch:03d}.h5', verbose=1)
 model.compile(loss=contrastive_loss, optimizer=rms, metrics=[accuracy])
 model.fit_generator(generator(imgs, batch_size), epochs=epochs, steps_per_epoch=iterations, callbacks=[checkpoint])

用了回調(diào)函數(shù)保存了每一個epoch后的模型,也可以保存最好的,之后需要對模型進(jìn)行測試。

測試時直接用load_model會報錯,而應(yīng)該變成如下形式調(diào)用:

model = load_model(model_path,custom_objects={'contrastive_loss': contrastive_loss }) #選取自己的.h模型名稱

emmm,到這里,就成功訓(xùn)練測試完了~~~寫的比較粗,因為這個代碼在官方給的mnist上的改動不大,只是方便大家用自己的數(shù)據(jù)集,大家如果有更好的方法可以提出意見~~~希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • python逆向微信指數(shù)爬取實現(xiàn)步驟

    python逆向微信指數(shù)爬取實現(xiàn)步驟

    這篇文章主要為大家介紹了python逆向微信指數(shù)爬取的實現(xiàn)步驟,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪
    2022-02-02
  • 使用Python pandas讀取CSV文件應(yīng)該注意什么?

    使用Python pandas讀取CSV文件應(yīng)該注意什么?

    本文是給使用pandas的新手而寫,主要列出一些常見的問題,根據(jù)筆者所踩過的坑,進(jìn)行歸納總結(jié),希望對讀者有所幫助,需要的朋友可以參考下
    2021-06-06
  • 利用python爬取斗魚app中照片方法實例

    利用python爬取斗魚app中照片方法實例

    最近在學(xué)習(xí)python,通過實踐是學(xué)習(xí)的一個好辦法,下面這篇文章就來給大家介紹了關(guān)于利用python爬取斗魚app中照片的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友下面來一起看看吧。
    2017-12-12
  • python使用遞歸的方式建立二叉樹

    python使用遞歸的方式建立二叉樹

    這篇文章主要介紹了python使用遞歸的方式建立二叉樹,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • Django admin實現(xiàn)TextField字段changelist頁面換行、空格正常顯示

    Django admin實現(xiàn)TextField字段changelist頁面換行、空格正常顯示

    本文主要介紹了Django admin實現(xiàn)TextField字段changelist頁面換行、空格正常顯示,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • Python報錯ModuleNotFoundError: No module named ‘tensorboard‘的解決方法

    Python報錯ModuleNotFoundError: No module named&

    在嘗試導(dǎo)入TensorBoard模塊時,你可能會遇到ModuleNotFoundError: No module named 'tensorboard'的錯誤,下面我們來分析這個問題并提供解決方案,需要的朋友可以參考下
    2024-09-09
  • Python中Tkinter Scrollbar滾動條(窗口滑動條)

    Python中Tkinter Scrollbar滾動條(窗口滑動條)

    本文主要介紹了Python中Tkinter Scrollbar滾動條(窗口滑動條),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • windows下安裝Python的XlsxWriter模塊方法

    windows下安裝Python的XlsxWriter模塊方法

    今天小編就為大家分享一篇windows下安裝Python的XlsxWriter模塊方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05
  • python?歸并排序的實現(xiàn)

    python?歸并排序的實現(xiàn)

    歸并排序是一種分治算法,它將數(shù)組分成兩半,分別對這兩半進(jìn)行排序,然后將排序后的兩半合并在一起,本文就來介紹一下python?歸并排序的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下
    2024-06-06
  • python淘寶搶購腳本程序?qū)崿F(xiàn)

    python淘寶搶購腳本程序?qū)崿F(xiàn)

    大家好,本篇文章主要講的是python淘寶搶購腳本程序?qū)崿F(xiàn),感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-02-02

最新評論

攀枝花市| 日土县| 盐山县| 台前县| 海门市| 九江市| 平安县| 根河市| 读书| 平安县| 千阳县| 万源市| 深州市| 遂宁市| 百色市| 滨海县| 米脂县| 改则县| 尉氏县| 青龙| 宕昌县| 来凤县| 响水县| 衡东县| 商丘市| 珠海市| 万全县| 巨鹿县| 温泉县| 乌审旗| 临邑县| 清新县| 吉隆县| 克山县| 和田市| 阿坝县| 九龙县| 海林市| 皮山县| 南安市| 陵川县|