import tensorflow as tf
from tensorflow import keras
class my_vgg13(tf.keras.Model):
def __init__(self):
super(my_vgg13, self).__init__()
self.con1 = tf.keras.layers.Conv2D(filters=64,kernel_size=[3, 3],padding='same',activation=tf.nn.relu),
self.con2 = tf.keras.layers.Conv2D(filters=64,kernel_size=[3, 3],padding='same',activation=tf.nn.relu),
self.pool1 = tf.keras.layers.MaxPool2D(pool_size=[2,2],strides=2,padding='same')
self.con3 = tf.keras.layers.Conv2D(filters=128, kernel_size=[3, 3], padding='same', activation=tf.nn.relu),
self.con4 = tf.keras.layers.Conv2D(filters=128, kernel_size=[3, 3], padding='same', activation=tf.nn.relu),
self.pool2 = tf.keras.layers.MaxPool2D(pool_size=[2, 2], strides=2, padding='same')
self.con5 = tf.keras.layers.Conv2D(filters=256, kernel_size=[3, 3], padding='same', activation=tf.nn.relu),
self.con6 = tf.keras.layers.Conv2D(filters=256, kernel_size=[3, 3], padding='same', activation=tf.nn.relu),
self.pool3 = tf.keras.layers.MaxPool2D(pool_size=[2, 2], strides=2, padding='same')
self.con7 = tf.keras.layers.Conv2D(filters=512, kernel_size=[3, 3], padding='same', activation=tf.nn.relu),
self.con8 = tf.keras.layers.Conv2D(filters=512, kernel_size=[3, 3], padding='same', activation=tf.nn.relu),
self.pool4 = tf.keras.layers.MaxPool2D(pool_size=[2, 2], strides=2, padding='same')
self.con9 = tf.keras.layers.Conv2D(filters=512, kernel_size=[3, 3], padding='same', activation=tf.nn.relu),
self.con10 = tf.keras.layers.Conv2D(filters=512, kernel_size=[3, 3], padding='same', activation=tf.nn.relu),
self.pool5 = tf.keras.layers.MaxPool2D(pool_size=[2, 2], strides=2, padding='same')
self.flaten = tf.keras.layers.Reshape(target_shape=(1*1*512,))
self.dens1 = tf.keras.layers.Dense(units=256, activation=tf.nn.relu)
self.dens2 = tf.keras.layers.Dense(units=128, activation=tf.nn.relu)
self.dens3 = tf.keras.layers.Dense(units=100, activation=None)
def call(self,inputs):
x = self.con1(inputs) #[b,32,32,3] => [b,32,32,64]
x = self.con2(x) #[b,32,32,64] => [b,32,32,64]
x = self.pool1(x) #[b,32,32,64] => [b,16,16,64]
x = self.con3(x)
x = self.con4(x)
x = self.pool2(x)
x = self.con5(x)
x = self.con6(x)
x = self.pool3(x)
x = self.con7(x)
x = self.con8(x)
x = self.pool4(x)
x = self.con9(x)
x = self.con10(x)
x = self.pool5(x) #=>[b,8,8,128]
x = self.flaten(x)
x = self.dens1(x)
x = self.dens2(x)
x = self.dens3(x) #[b,100]
#out = tf.nn.softmax(x)
return x
conv_net=my_vgg13()
optimizer = tf.keras.optimizers.Adam(lr=0.001)
(x,y),(x_test,y_test)=tf.keras.datasets.cifar100.load_data()
#print(x.shape,y.shape,x_test.shape,y_test.shape)
def preprocess(x,y):
x=tf.cast(x,dtype=tf.float32)/255.
y=tf.cast(y,dtype=tf.int32)
return x,y
y=tf.squeeze(y,axis=1)
y_test=tf.squeeze(y_test,axis=1)
#print(x.shape,y.shape,x_test.shape,y_test.shape)
train_db=tf.data.Dataset.from_tensor_slices((x,y))
train_db=train_db.map(preprocess).shuffle(1000).batch(128)
test_db=tf.data.Dataset.from_tensor_slices((x_test,y_test))
test_db=test_db.map(preprocess).batch(128)
sample=iter(train_db)
sample_1=next(sample)
#print(sample_1[0].shape)
#print(sample_1[1].shape)
def main():
for epoch in range(10):
for step, (x, y) in enumerate(train_db):
with tf.GradientTape() as tape:
# [b,32,32,3]=>[b,1,1,512]
out = conv_net(x)
# flatten,=>[b,512]
#out = tf.reshape(out, [-1, 512])
#logits = fc_net(out)
y_onehot = tf.one_hot(y, depth=100)
loss = tf.losses.categorical_crossentropy(y_onehot, out, from_logits=True)
loss = tf.reduce_mean(loss)
grads = tape.gradient(loss, conv_net.trainable_variables)
optimizer.apply_gradients(zip(grads, conv_net.trainable_variables))
if step % 100 == 0:
print(epoch, step, "loss:", float(loss))
total_num = 0
total_correct = 0
for x, y in test_db:
out = conv_net(x)
#out = tf.reshape(out, [-1, 512])
#logits = fc_net(out)
prob = tf.nn.softmax(out, axis=1)
prob = tf.argmax(prob, axis=1)
prob = tf.cast(prob, dtype=tf.int32)
correct = tf.equal(prob, y)
correct = tf.reduce_sum(tf.cast(correct, dtype=tf.int32))
# correct=tf.cast(tf.equal(prob,y),dtype=tf.int32)
total_correct += int(correct)
total_num += x.shape[0]
acc = total_correct / total_num
print(epoch, "test acc", acc)
if __name__=="__main__":
main()
運行報錯:
Traceback (most recent call last):
File "C:/Users/dell/Desktop/2017010502/self_vgg_13.py", line 96, in main
out = conv_net(x)
File "C:\ProgramData\Anaconda3\envs\tf2\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py", line 822, in __call__
outputs = self.call(cast_inputs, *args, **kwargs)
File "C:/Users/dell/Desktop/2017010502/self_vgg_13.py", line 35, in call
x = self.con1(inputs) #[b,32,32,3] => [b,32,32,64]
TypeError: '_TupleWrapper' object is not callable
怎么解決
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/38554.html
上一篇:SiamRPN 實作目標檢測
