' W0 A3 V# l, f3 g* r- S9 Qsrc_w=src_h=5- w2 l T6 E- C9 X* q
dst_w=dst_h=10 : N0 m P; b: j 3 [& K1 T3 \' ?! \8 c7 q. ndst_x=dst_y=5 7 ?6 |! t/ [4 u# `src_x=dst_x*(src_h/dst_h)$ g1 o: M. b2 \* ]) y
src_y=dst_y*(src_w/dst_w). r$ B# N5 y5 H
6 o! _* f, Z" a- E' P7 r# m2 V) s
i=math.floor(src_x) 4 z. K$ l' [" fj=math.floor(src_y)# L: b# {% p S+ z6 T+ l& s. b
) f5 B0 `( f# z! J. }u=src_x-i : E3 P+ y3 I2 k2 z* fv=src_y-j + X) H2 z0 m# B7 R# print(i,j,u,v)0 `4 q# Y0 Q J- R& [* J* M- K. ~
# l_x=np.array([[1.5,0.5,0.5,1.5]])+ ` N, Z$ \$ S9 q
# l_y=np.array([[1.5,0.5,0.5,1.5]])5 _$ C. p7 I R+ g
base=[1,0,-1,-2]* L% Y2 x! F1 z l' }7 n
. v" L! Z7 ?( T# z$ u* u : P& ?. R! h+ Z# ?8 I2 jdef l_(r):% c1 d: k9 Z2 y I6 F$ I
a=np.zeros((1,4)) 5 Z( u2 [" t: u2 [) r& h for j in range(4): 6 a" [/ {$ }6 H: a a[0,j]=r+base[j]9 o" H9 i! x; J f4 C; j! w" d
return a |" E$ k0 x6 K% _" M' J6 M % x" L$ m/ U- ]4 ^7 K) F8 R* `/ U( E/ `9 K3 l6 f/ o
l_x,l_y=l_(u),l_(v)) ?, C' k1 f" P7 v
# print(l_x); Z* G# O; N R3 t
# print(l_y) 1 j* G2 u4 W( A( O8 w* i ' \4 ]" j+ s8 g+ y% V# # print(l_x.shape) ! N" A# R5 ]" x. N5 B" }% C#& U9 J: s5 ?0 S! O9 k9 Q( C9 b
# : r# {0 l l5 @def S_x(l_):+ B% c) X2 |4 U7 Q
s=np.zeros(shape=(1,4)) 2 @% s: z- e: W, b5 G, U% | for j in range(4): R8 i( V4 E; \* f9 _1 ]
x_abs=math.fabs(l_[0,j]) 7 n9 X6 N! r. [' c' T$ p; C if x_abs <= 1:. `+ K1 W& S$ M. a5 A; r$ H+ K7 M
s[0,j] = 1-2* math.pow(x_abs, 2)+math.pow(x_abs, 3) 3 V* D- W# W" C9 | elif x_abs < 2 and x_abs > 1: 9 Y) R4 t2 \! L' d' d* f s[0,j] = 4-8*x_abs+5*math.pow(x_abs, 2) - math.pow(x_abs, 3) : s' f- B5 w( h' h1 [! C6 L2 h% @ return s& g( G7 p1 o: ]* F a
# i6 t6 T, Z# T g; B4 `
1 B9 W) H# ?% }# i* f
A,C=S_x(l_x),S_x(l_y)9 I3 r4 g L9 G( Z5 s* b+ ^
- c& Y. r) a* q# n$ v8 E
B=np.array([% y- V( |1 q8 z3 m$ I* a
[9., 0., 7., 3.]," F+ C) f- m$ U4 A
[7., 0., 1., 8.], 9 F5 H+ g! {* c, h2 f% D [1., 8., 1., 3.],2 b9 t* v# G0 k& v- y" t0 q( T
[5., 5., 1., 1.]]) , f+ f4 L7 z6 k' O, Xb=np.matmul(np.matmul(A,B),np.transpose(C))( W) e) I' \$ g; j! R: Y# R
print(b)) E4 i: P. f9 M( c! | z, g) p
19 {0 w; l0 i& K5 Y% Z( }8 f
2 / @" T! |( v2 `& e+ ~4 o3 . p$ Q9 `& X% e9 X44 X0 c0 v4 R. e+ i
52 d" d2 {# G5 l9 Y6 d5 }
6 % x/ T" k) v. O0 y7" ]" b4 @# d1 d- X
8' b, e- n9 w0 z, {+ A4 n; W+ i
9 8 ]0 Z1 B O3 p4 ?8 M10 5 |5 W8 x0 _ K y! T# V- a) O" W& r11 # P; E3 M# A6 N+ C3 ~12) X1 m5 S+ M. ]
13 8 c: h+ Z1 ~( d9 b3 G5 r# h; K14* M {2 B; C, X! M8 |9 q
15: a# J' R; X6 k
16) m. D Z$ k ?9 @& A! y
17 7 d! o) @6 w. ?18' e( ^: O; i. O1 Z+ e( ^9 z' m
19 6 V1 ]# j% R% J- K3 V20$ \2 @: I* b1 t& G1 E6 v
21 * V+ \: U6 T/ d, j" ^22$ U2 f, x2 J6 d
23 9 v9 ?$ M. a6 d$ e1 v9 L. T) L" z& ~9 c24& x2 L- Q7 ]+ o1 F
259 S) P) w/ T/ i# s) E6 S( k. j
26& r2 l& I" e: j6 U" r
27 / O0 b. x8 a F5 x28 : D( f0 a$ [8 o+ U) {* Y29' \6 D5 R G/ D* _! f: ]! E- W
30 I8 @$ J9 b4 W! ~: d4 q; f31 % m/ d S" m$ P% r+ \! X, s32 ! Q& q2 u/ E& b5 I33) L- g7 T" r' Y4 f. q$ [
34 0 T! g5 D$ P: d35$ j1 E& `# ^1 l3 {' Q+ V7 Y$ e
36% W% O3 q# ^3 n# K
37 `/ k/ w8 `- ?, q: C38 # f* q2 e D h! D2 B. V39 6 r. G( N# ?' M* c- y40 5 p6 b: `8 \( |41 0 p* a- `# ~* C) X" g p42 + e1 `4 i* P6 R- r* T( V) t& T/ x6 @43 X( L, `" M9 {8 {' p/ _8 X I+ r44 # U0 o) t) n6 Z. g4 U45" ~0 O% J' T6 t7 f! F( ]* b
46 ! U) R6 [ Y; }) F" B. w9 W! H; V472 B6 s7 m5 X' v- F9 x( v. _
48 % q& v! C" @$ l `+ u49 ( f7 g! B" y3 z508 l* s3 C& I& c" ]9 i0 _, E- l
51 V! C: q& \# r( A- v, R, y6 D
52 @. Q; @4 y+ i% ?3 X" v53 ; n3 Q- K6 H; R$ K4 G! u# _54( v0 a+ Y/ f- D' j0 o1 h) j
55 # A% c7 m. a" L. t x" P- K3.代码实现) ]; ?% P% L' U& G! { Q3 S
import torch: F. n$ h' r: D1 I0 `$ m
import torch.nn as nn . @5 X% `! O4 _* Q, \1 h" }+ Ofrom torchvision import transforms4 e: O# W: N! ~9 l: w
; b J/ F7 c1 L. G5 c2 B1 L4 z 8 _- x [3 d$ \, z$ Cimg=torch.randint(10,size=(1,4,4),dtype=torch.float32)) m+ n8 U) Z* j0 M4 e
print(img) 2 ?$ |/ A) Q* ]% K5 ^# Z0 I% wprint('---'*5) - {4 E2 V5 w- G3 @4 q# s# z3 Nbicubic_interpolation=transforms.Resize(size=(8,8),* G5 @7 b2 v' ]# V+ b
interpolation=transforms.InterpolationMode.BICUBIC); ]& w$ U; G3 y \0 T0 d: A
resize_img=bicubic_interpolation(img)* ^+ L8 F$ P9 b1 l8 _$ a4 y( ^* s
print(resize_img)$ c( [) S9 L0 ]
1 ! A P g4 `7 M$ u0 x2 0 D- S4 H' U9 P3% g1 R7 S- r7 l8 Y
4. o* d* R! c% O& V
5 $ P' Z8 J+ a! b9 D+ S( E, H* @- r' v61 Q* n8 d, ?% ~$ c/ r2 a/ P
7 0 p) \0 v0 _- K/ H8; `0 z' P& U* g! N; x3 m
9 ) K Y+ R; `9 f9 ?10' M6 e* t( u; d7 ]# V/ U
11 + l6 c5 k a4 ?: o* e' r12' j* R- t2 z3 L8 [$ o1 {) g
四、Pytorch实现 * B6 f+ s V; |; q5 _1 T. t) R) @! p. W大家注意在Pytorch中不同插值方法是通过对transforms.Resize方法中的interpolation参数指定,上面这三种方法只需要依次采用下面三种模式即可: 9 W; [5 F0 l% I$ { 5 o& A1 [4 X# {- l插值方法 指定方式 3 z/ Y" }( h7 F9 A最近邻插值 transforms.InterpolationMode.NEAREST ) q z1 g+ R: ^4 x( k5 y' }: q8 o双线性插值 transforms.InterpolationMode.BILINEAR 1 h) F. l& A0 \+ \2 I双三次插值 transforms.InterpolationMode.BICUBIC! ]2 U% G: N. X4 G- Y2 [
这里我使用周杰伦的一张照片展示一下三种方法对应的插值结果: " L; D% ` S& Z! F原图如下: 8 A6 ?# ~& `+ P1 X) u4 W9 |2 q; @/ G$ U
$ R5 J) D7 f& c$ e
import torch ) ~. W& m- M0 A( l* L, E5 Ffrom torchvision import transforms$ [3 P& k# G+ k& E3 w4 l
from PIL import Image6 R. F9 s' d, x( t3 | }/ X
from torchvision.utils import save_image' d5 y8 I, K6 ^& M! z
) ~: Q/ Z+ L0 T
0 Q7 r/ B/ y- q( timg=Image.open('./Jay.png',mode='r')& ]* F- I# X( ?, s+ K9 O; g8 M; h. C. K
img_to_tensor=transforms.ToTensor()(img)- O+ z. l. M, T& n! ^0 _9 G
# print(img_to_tensor.shape)) T9 B6 k8 L2 z4 R