init = random.sample(range(num), numInjections + offset)
model.load_weights('h5/fcn-trained.h5')

for i in init:
    test_loss, test_acc = model.evaluate(test_images[i:i + 1],
                                         test_labels[i:i + 1],
                                         verbose=0)
    if (test_acc == 1.):
        ind.append(i)
ind = ind[:numInjections]

start = time.time()
for i in range(numFaults):
    model.load_weights('h5/fcn-trained.h5')

    tfi.inject(model=model, confFile=conf)

    sdc = 0.
    for i in ind:
        test_loss, test_acc = model.evaluate(test_images[i:i + 1],
                                             test_labels[i:i + 1],
                                             verbose=0)
        if (test_acc == 0.):
            sdc = sdc + 1.

    f.write(str(sdc / numInjections))
    f.write("\n")
    totsdc = totsdc + sdc
f.write("\n")
f.write(str(totsdc / (numFaults * numInjections)))
f.write("\n")
numFaults = int(sys.argv[3])
numInjections = 10
#numInjections = int(sys.argv[4])

totsdc = 0.0

start = time.time()
for i in range(numFaults):
    model.load_weights('h5/vgg16-trained.h5')
    sdc = 0.
    for i in ind:
        image = load_img(images[i], target_size=(224, 224))
        image = img_to_array(image)
        image = image.reshape(
            (1, image.shape[0], image.shape[1], image.shape[2]))
        image = preprocess_input(image)
        res = tfi.inject(model=model, x_test=image, confFile=conf)
        label_ = decode_predictions(res[0])
        label_ = label_[0][0]
        if (label_[1] != img_labels[i]):
            sdc = sdc + 1.
    f.write(str(sdc / numInjections))
    f.write("\n")
    totsdc = totsdc + sdc
f.write("\n")
f.write(str(totsdc / (numFaults * numInjections)))
f.write("\n")
f.write("Time for %d injections: %f seconds" %
        (numFaults * numInjections, time.time() - start))
f.close()
Exemple #3
0
num = test_images.shape[0]

totsdc = 0.0

ind = []
init = random.sample(range(num), numInjections+offset)
model.load_weights('h5/nn-trained.h5')

for i in init:
        test_loss, test_acc = model.evaluate(test_images[i:i+1], test_labels[i:i+1], verbose=0)
        if(test_acc == 1.):
                ind.append(i)
ind = ind[:numInjections]

start = time.time()
for i in range(numFaults):
    model.load_weights('h5/nn-trained.h5')
    sdc = 0.
    for i in ind:
        res = tfi.inject(model=model, x_test=test_images[i:i+1], confFile=conf)
        if(res != test_labels[i:i+1]):
            sdc = sdc + 1.
    f.write(str(sdc/numInjections))
    f.write("\n")
    totsdc = totsdc + sdc
f.write("\n")
f.write(str(totsdc/(numFaults*numInjections)))
f.write("\n")
f.write("Time for %d injections: %f seconds" % (numFaults*numInjections, time.time() - start))
f.close()
    test_ = test_ds.take(2)
    test_loss, test_acc = model.evaluate(test_, verbose=0)
    if(test_acc == 1.):
        break

for i in range(20):
    test_i = test_ds.take(1)
    test_loss, test_acc = model.evaluate(test_i, verbose=0)
    if(test_acc == 1.):
        test_ = test_.concatenate(test_i)
    if(tf.data.experimental.cardinality(test_).numpy() == 10):
        break

start = time.time()
print(start)
for i in range(numFaults):
    model.load_weights('h5/alexnet-trained.h5')
    sdc = 0.
    for elem, label in test_.as_numpy_iterator():
        res = tfi.inject(model=model, x_test=elem, confFile=conf)
        if(res != label):
            sdc = sdc + 1.
    f.write(str(sdc/numInjections))
    f.write("\n")
    totsdc = totsdc + sdc
f.write("\n")
f.write(str(totsdc/(numFaults*numInjections)))
f.write("\n")
f.write("Time for %d injections: %f seconds" % (numFaults*numInjections, time.time() - start))
f.close()