运行程序输入:
import input_emotion_data
data = input_emotion_data.read_data(input_dir = 'emotion')
import cnnemotion
cnnemotion.run_training(data=data, n_training_data=data.train.images.shape[0],
is_CAENN=False, batch_size=170, n_training_epochs = 10, n_cae_training_epochs=5)
input_emotion_data.py
def read_data(input_dir, cut64x64=False, test_rate = 0.2, one_hot=True):
temp_dir = path.join(input_dir, 'temp')
os.path.join()
os.path.join()是在拼接路径时使用的,上句返回路径"emotion/temp"
在运行语句"import input_emotion_data"后,在emotion文件夹里生成temp文件夹,但temp里面是空的。
在运行语句“data = input_emotion_data.read_data(input_dir = 'emotion')”后,temp文件变成如下:
并且输出了一些one-hot数组和内容是浮点数的数组。
它们究竟是什么?我猜是labels和images~不过images长得形状有点奇怪,要好好研究下它为什么长那样。
# 如果序列化文件存在,则直接将其数据反序列化读入data中,然后函数返回data即可。
filename = "Emotion_pic_cut64x64_%s_rate_%.1f_onehot_%s.pickle" % (str(cut64x64), test_rate, str(one_hot))
if path.exists(path.join(temp_dir, filename)):
with open(path.join(temp_dir, filename), 'rb') as f_pickle:
data = pickle.load(f_pickle)
print("type(data)")
print(type(data))
return data
# 否则,继续运行read_data()函数下面的其他语句
pic_with_anger_dir = path.join(temp_dir, 'anger')
pic_with_happiness_dir = path.join(temp_dir, 'happiness')
pic_with_sadness_dir = path.join(temp_dir, 'sadness')
pic_with_calm_dir = path.join(temp_dir, 'calm')
if not path.exists(temp_dir):
os.mkdir(temp_dir)
序列化
在程序运行的过程中,所有的变量都是在内存中,但是一旦程序结束,变量所占用的内存就被操作系统全部回收。
我们把变量从内存中变成可存储或传输的过程称之为序列化,在Python中叫pickling。
序列化之后,就可以把序列化后的内容写入磁盘,或者通过网络传输到别的机器上。
反过来,把变量内容从序列化的对象重新读到内存里称之为反序列化,即unpickling。
pickle.load(file):注解:从file中读取一个字符串,并将它重构为原来的python对象。
# unzip
zipfile.ZipFile(path.join(input_dir, 'anger.zip')).extractall(path=temp_dir)
zipfile.ZipFile(path.join(input_dir, 'happiness.zip')).extractall(path=temp_dir)
zipfile.ZipFile(path.join(input_dir, 'sadness.zip')).extractall(path=temp_dir)
zipfile.ZipFile(path.join(input_dir, 'calm.zip')).extractall(path=temp_dir)
# get all pictures filename
pic_with_anger_filenames = os.listdir(pic_with_anger_dir)
pic_with_happiness_filenames = os.listdir(pic_with_happiness_dir)
pic_with_sadness_filenames = os.listdir(pic_with_sadness_dir)
pic_with_calm_filenames = os.listdir(pic_with_calm_dir)