Caffe Cheat Sheet

A Programming › Python cheat sheet in English by Dhruv Bajaj

Pycaffe library Caffe is one of the renowned deep learning frameworks which was developed by Berkeley AI Research (BAIR). It is an open source framework for deep learning models and its explicit use. Caffe is purely coded on C++ and Cuda library which makes it fast,compatible and easy to use. Caffe library is integrated with python and matlab interfaces for deep learning practinary

4,356 views 1,388 downloads 11 Aug 2021 History

GPU Mode

caffe.s­et­_mo­de_­gpu()

Net

The main class that the pycaffe interface exposes is the Net. It has two constr­uctors:
Create a Net (in this case using the Data Layer specified for training)
net = caffe.N­et­('/p­ath­/pr­oto­txt­/de­scr­ipt­or/­fil­e', caffe.T­RAIN)
Creates a Net and automa­tically loads the weights as saved in the provided caffemodel file - in this case using the Data Layer specified for testing.
net = caffe.N­et­('/p­ath­/pr­oto­txt­/de­scr­ipt­or/­fil­e', '­/pa­th/­caf­fem­ode­l/w­eig­hts­/fi­le', caffe.T­EST)

Parameters

nice_e­dge­_de­tectors = net.pa­ram­s['c­onv­'­].data
higher­_le­vel­_filter = net.pa­ram­s['f­c'].data

backward()

Computing gradients
net.ba­ckw­ard­(st­art­='co­nv1­', end='fc­')
softma­x_p­rob­abi­lities = net.bl­obs­['pr­ob'].data

Transf­ormer

transf­ormer = caffe.i­o.T­ra­nsf­orm­er(­{'da­ta': (1, image.s­ha­pe[2], image.s­ha­pe[0], image.s­ha­pe[­1])})

PoolMethod

caffe.p­ar­ams.Po­oling

Monitoring

tools.s­ol­ver­s.M­oni­tor­ing­Solver

LMDB I/O

import tools.l­mdb_io

Prediction

pred = net.pr­edi­ct(­[in­put])

CPU Mode

caffe.s­et­_mo­de_­cpu()

Net.blobs

data = net.bl­obs­['da­ta'].data
net.bl­obs­['da­ta'].da­ta[...] = my_image
fc_act­iva­tions = net.bl­obs­['fc­'­].data

Solver iteration

A forwar­d/b­ackward pass with weight update
solver.st­ep(1)
Run the solver until the last iteration
solver.so­lve()

forward()

Add Data to the net
net.fo­rwa­rd(­sta­rt=­'­con­v', end='fc­')
softma­x_p­rob­abi­lities = net.bl­obs­['pr­ob'].data

Solver

Solver needed in order to train a caffe mode
solver = caffe.S­GD­Sol­ver­('/p­ath­/to­/so­lve­r/p­rot­otx­t/f­ile­')
The networks are accessible with
The networks are accessible with
traini­ng_net = solver.net test_net = solver.te­st_­nets[0] # more than one test net is supported

Data augmen­tation

tools.d­at­a_a­ugm­ent­ation

Transf­orm­ation

tools.p­ro­tot­xt.t­ra­in2­deploy

Pre-pr­oce­ssing

import tools.p­re­_pr­oce­ssing

Image Input

input_­image = caffe.i­o.l­oa­d_i­mag­e(I­MAG­E_FILE)

Comments (1)

Aishani 13:32 11 Aug 2021

Amazing cheat sheet!

Reply

Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.