I am working on how to create HDF5 file for age and gender image recognition datasets. I want to produce an HDF5 file for the aforementioned dataset so that I may utilise it .h5 file for image recognition in a webcam application.
I made a.h5 file.
‘’’
import h5py as h5
def train_model(model, dataloaders, optimizer, criterion1, criterion2, num_epochs):
since = time.time()
history = {
#'train_acc': [],
'train_loss': [],
'val_loss': [],
'age_accuracy': [],
'gender_accuracy': []
}
for epoch in range(num_epochs):
print("Epoch {}/{}".format(epoch, num_epochs-1))
print("-" * 10)
for phase in ["train", "test"]:
running_loss = 0.0
running_acc1 = 0.0
running_acc2 = 0.0
total_1 = 0
total_2 = 0
if phase == "train":
model.train()
else:
model.eval()
for inputs, label1, label2 in tqdm(dataloaders[phase]):
inputs = inputs.to(device)
label1 = label1.long()
label2 = label2.long()
label1 = label1.to(device)
label2 = label2.to(device)
optimizer.zero_grad()
with torch.autograd.set_grad_enabled(phase=="train"):
outputs = model(inputs)
loss1 = criterion1(outputs[0], label1)
loss2 = criterion2(outputs[1], label2)
loss = loss1 #+ loss2
_, preds1 = torch.max(outputs[0], 1)
_, preds2 = torch.max(outputs[1], 1)
if phase == "train":
loss.backward()
optimizer.step()
running_loss += loss.item() * inputs.size(0) #
running_acc1 += torch.sum(preds1 == label1.data)
running_acc2 += torch.sum(preds2 == label2.data)
total_1 += label1.size(0)
total_2 += label2.size(0)
epoch_loss = running_loss / len(dataloaders[phase].dataset)
age_epoch_acc1 = 100 * running_acc1 // total_1
gender_epoch_acc2 = 100 * running_acc2 // total_2
print("{} Loss: {:.4f} Age_Acc: {:.4f} gender_Acc: {:.4f} ".format(phase, epoch_loss, age_epoch_acc1, gender_epoch_acc2)) #, gender_epoch_acc2 "gender_Acc: {:.4f}"
if phase == 'test':
history['age_accuracy'].append(age_epoch_acc1)
history['gender_accuracy'].append(gender_epoch_acc2)
history['val_loss'].append(epoch_loss)
if phase == 'train':
history['train_loss'].append(epoch_loss)
print()
torch.save(model, '/content/drive/MyDrive/ageNgendermodel.h5')
time_elapsed = time.time() - since
print("Training compete in {:.0f}m {:.0f}s".format(time_elapsed // 60, time_elapsed % 60))
return model, history
‘’’
My “ageNgendermodel.h5” file is created in Google Drive after training the model. However, when I download this file and utilise it in a web cam programme in a Jupyter notebook, I get the message “OSError: Unable to open file (File signature not found).”
I used the following code to determine if an HDF5 file was valid or not:
‘’’
import h5py
if h5py.is_hdf5('/content/drive/MyDrive/ageNgendermodel.h5'):
print("File is a valid HDF5 file")
else:
print("File is not a valid HDF5 file")
‘’’
for above code it shows “File is not a valid HDF5 file”.
I’m not sure where my code is going wrong. I’ve been attempting to address this problem for two days and haven’t been successful.
Please assist me in resolving this issue.