Пример #1
0
 def forward(self, x, only_encode=False):
     feats, additional = self.backbone(x)
     feature_pyramid = upsample(feats, self.size)
     features = feature_pyramid
     logits = self.logits.forward(features)
     #out = upsample(logits, self.size)
     out = upsample(logits, (692, 2048))
     return out
Пример #2
0
    def forward(self, img):

        img_t0, img_t1 = torch.split(img, 3, 1)
        features_t0 = self.encoder1(img_t0)
        features_t1 = self.encoder2(img_t1)
        features = features_t0 + features_t1
        features_map = self.attention_module(features)
        pred_ = self.decoder(features_map)
        pred_ = upsample(pred_, [pred_.size()[2] * 2, pred_.size()[3] * 2])
        pred_ = self.bn(pred_)
        pred_ = upsample(pred_, [pred_.size()[2] * 2, pred_.size()[3] * 2])
        pred_ = self.relu(pred_)
        pred = self.classifier(pred_)

        return pred
Пример #3
0
def processOutput(output, loopback_Fs, loopback_Fc, upsample_factor, mask_noise, percentile=95):
    output = util.upsample(output, upsample_factor)

    ramp_length = int(loopback_Fs*.01)
    output[:ramp_length] *= np.clip(np.arange(ramp_length, dtype=float) / ramp_length, 0, 1)
    output[-1:-ramp_length-1:-1] *= np.clip(np.arange(ramp_length, dtype=float) / ramp_length, 0, 1)

    carrier = np.exp(1j * 2 * np.pi * np.arange(output.size) * loopback_Fc / loopback_Fs)
    y1 = (output * carrier).real
    for i in xrange(13):
        y1 = np.diff(np.r_[0,y1])

    peak = np.percentile(np.abs(y1), percentile)
    gain = 16.0 / peak if peak else 0.
    output = gain * y1
    if 0:
        limit = 800.
        output = (2/np.pi*limit)*np.arctan((np.pi/2/limit)*output)
    output = np.r_[output, np.zeros(int(.05*loopback_Fs))]
    if mask_noise is not None:
        if mask_noise.size > output.size:
            output = np.r_[output, np.zeros(mask_noise.size-output.size)]
        output[:mask_noise.size] += mask_noise[:output.size]
    delay_samples = int(delay*loopback_Fs)
    # delay one channel slightly relative to the other:
    # this breaks up the spatially-dependent frequency-correlated
    # nulls of our speaker array
    output = np.vstack((np.r_[np.zeros(delay_samples), output],
                        np.r_[output, np.zeros(delay_samples)])).T
    global waveform
    waveform = output
    return output
Пример #4
0
 def forward(self, pyramid, image_size):
     features, additional = self.backbone(pyramid)
     if self.has_aux_logits:
         additional['aux_logits'] = self.aux_logits.forward(
             additional['upsamples'][0])
     logits = self.logits.forward(features)
     return upsample(logits, image_size), additional
def main():
    opts = util.parse_args()
    X, y = util.data_load(opts.dataset)

    fc_nn_model = fc.create_model()
    ada_model = AdaBoostClassifier(n_estimators=100, random_state=0)
    svm_model = SVC(C=1000, gamma=0.1)

    n = opts.upsamplen if opts.upsamplen is not None else 1
    start = n if opts.upsamplestart is None else 1

    if start > n:
        print("unsample range error")
        sys.exit()
    conf_fc, conf_ada, conf_svm = [], [], []
    for t in np.arange(start, n + 1):
        needed = util.needed_n(X, y, t)
        temp_X, temp_y = util.upsample(X, y, needed)
        X_train, X_test, y_train, y_test = train_test_split(temp_X,
                                                            temp_y,
                                                            test_size=0.3,
                                                            random_state=42)
        X_train, X_test = util.normalize(X_train, X_test)
        train_dset = tf.data.Dataset.from_tensor_slices(
            (X_train,
             y_train)).batch(64,
                             drop_remainder=False).shuffle(buffer_size=10000)
        test_dset = tf.data.Dataset.from_tensor_slices(
            (X_test, y_test)).batch(64)
        ada_model.fit(X_train, y_train)
        svm_model.fit(X_train, y_train)
        fc_nn_model.fit(train_dset, epochs=10)
        pred_ada = ada_model.predict(X_test)
        pred_svm = svm_model.predict(X_test)
        conf_ada.append(confusion_matrix(y_test, pred_ada))
        conf_svm.append(confusion_matrix(y_test, pred_svm))
        temp = np.zeros((2, 2), dtype=int)
        for d, labels in test_dset:
            predictions = fc_nn_model(d)
            for i in range(len(d)):
                temp[labels[i]][np.argmax(predictions[i])] += 1
        conf_fc.append(temp)
    recall_fc = list(map(lambda x: util.recall(x), conf_fc))
    recall_ada = list(map(lambda x: util.recall(x), conf_ada))
    recall_svm = list(map(lambda x: util.recall(x), conf_svm))
    up_range = np.arange(start, n + 1)
    d = {"SVM": recall_svm, "Adaboost": recall_ada, "FC_NN": recall_fc}
    legends = ["SVM", "Adaboost", "FC_NN"]
    for key in d:
        plt.plot(up_range, d[key])
    plt.title("Recall Vs Upsample Graph")
    plt.xlabel("Upsample rate")
    plt.ylabel("Recall")
    plt.legend(legends)
    plt.show()
Пример #6
0
def main():
    opts = util.parse_args()
    X, y = util.data_load(opts.dataset)
    model = create_model()
    model_layers = create_model_more_layers()
    n = opts.upsamplen if opts.upsamplen is not None else 1
    start = n if opts.upsamplestart is None else 1
    all_conf = []
    all_conf_layers = []
    if start > n:
        print("Upsample start should be larger than end")
        sys.exit()
    for t in np.arange(start, n + 1):
        print("t", t)
        needed = util.needed_n(X, y, t)
        temp_X, temp_y = util.upsample(X, y, needed)
        X_train, X_test, y_train, y_test = train_test_split(temp_X,
                                                            temp_y,
                                                            test_size=0.3,
                                                            random_state=42)
        X_train, X_test = util.normalize(X_train, X_test)
        train_dset = tf.data.Dataset.from_tensor_slices(
            (X_train,
             y_train)).batch(64,
                             drop_remainder=False).shuffle(buffer_size=10000)
        test_dset = tf.data.Dataset.from_tensor_slices(
            (X_test, y_test)).batch(64)
        model.fit(train_dset, epochs=10)
        model_layers.fit(train_dset, epochs=10)
        conf_mat = np.zeros((2, 2), dtype=int)
        conf_mat_layers = np.zeros((2, 2), dtype=int)
        for d, labels in test_dset:
            predictions = model(d)
            predictions_layers = model_layers(d)
            for i in range(len(d)):
                conf_mat[labels[i]][np.argmax(predictions[i])] += 1
                conf_mat_layers[labels[i]][np.argmax(
                    predictions_layers[i])] += 1
        all_conf.append(conf_mat)
        all_conf_layers.append(conf_mat_layers)
    re_fc, re_fc_layer = list(map(lambda x: util.recall(x), all_conf)), list(
        (map(lambda x: util.recall(x), all_conf_layers)))
    up_range = np.arange(start, n + 1)
    plt.plot(up_range, re_fc)
    plt.plot(up_range, re_fc_layer)
    plt.title("2-layer NN vs 3-layer NN")
    plt.legend(["2-layer", "3-layer"])
    plt.xlabel("Upsample rate")
    plt.ylabel("Recall")
    plt.show()
Пример #7
0
def processOutput(output, loopback_Fs, loopback_Fc, upsample_factor, mask_noise):
    output = util.upsample(output, upsample_factor)
    output = (output * np.exp(1j * 2 * np.pi * np.arange(output.size) * loopback_Fc / loopback_Fs)).real
    output *= 1.0 / np.percentile(np.abs(output), 95)
    output = np.r_[output, np.zeros(int(.1*loopback_Fs))]
    if mask_noise is not None:
        if mask_noise.size > output.size:
            output = np.r_[output, np.zeros(mask_noise.size-output.size)]
        output[:mask_noise.size] += mask_noise[:output.size]
    delay_samples = int(delay*loopback_Fs)
    # delay one channel slightly relative to the other:
    # this breaks up the spatially-dependent frequency-correlated
    # nulls of our speaker array
    output = np.vstack((np.r_[np.zeros(delay_samples), output],
                        np.r_[output, np.zeros(delay_samples)])).T
    return output
Пример #8
0
def main():
    opts = util.parse_args()
    X, y = util.data_load(opts.dataset)
    #set upsample range
    n = opts.upsamplen if opts.upsamplen is not None else 1
    start = n if opts.upsamplestart is None else 1

    if start > n:
        print("unsample range error")
        sys.exit()
    for i in np.arange(start, n + 1):
        print("i", i)
        needed = util.needed_n(X, y, i)
        print("needed", needed)
        print("pre-upsampled len x", len(X))
        upsampled_X, upsampled_y = util.upsample(X, y, needed)
        print("length of upsampled_X", len(upsampled_X))
        #use a good param to improve speed
        X_train, X_test, y_train, y_test = train_test_split(upsampled_X,
                                                            upsampled_y,
                                                            test_size=0.3,
                                                            random_state=42)
        X_train, X_test = util.normalize(X_train, X_test)
        model = SVC(C=1000, gamma=0.1)
        model.fit(X_train, y_train)
        predictions = model.predict(X_test)
        cm = confusion_matrix(y_test, predictions)
        report = classification_report(y_test, predictions)

        #precision-recall curve
        y_score = model.decision_function(X_test)
        average_precision = average_precision_score(y_test, y_score)
        disp = plot_precision_recall_curve(model, X_test, y_test)
        disp.ax_.set_title('Precision-Recall curve with 10x upsampling: '
                           'AP={0:0.2f}'.format(average_precision))
        plt.show()

        #roc curve
        # svc_roc=plot_roc_curve(model,X_test,y_test)
        # svc_roc.ax_.set_title('SVM ROC Curve with 10x upsampling')
        # plt.show()

        print(cm)
        print(report)
    '''
Пример #9
0
def channelModel(output, lsnr):
    # received symbols
    input = np.copy(np.r_[output, np.zeros(32)])
    freq_offset = 232e3 * (2*np.random.uniform()-1)
    input *= np.exp(2*np.pi*1j*freq_offset*np.arange(input.size)/20e6)
    phase_offset = 2*np.pi*np.random.uniform()
    input *= np.exp(1j*phase_offset)
    # random causal 16-tap FIR filter with exponential tail
    h = (np.random.standard_normal(16) + 1j*np.random.standard_normal(16)) * np.exp(-np.arange(16)*5.4/16)
    h[0] += 4.
    h /= np.sum(np.abs(h)**2)**.5
    input = np.convolve(input, h, 'same')
    input = util.upsample(input, 16)
    time_offset = np.random.random_integers(0, 15)
    input = input[time_offset::16]
    snr = 10.**(.1*lsnr)
    input = add_noise(input, np.var(input)*64./52./snr)
    return input
Пример #10
0
def processOutput(output, loopback_Fs, loopback_Fc, upsample_factor,
                  mask_noise):
    output = util.upsample(output, upsample_factor)
    output = (output * np.exp(1j * 2 * np.pi * np.arange(output.size) *
                              loopback_Fc / loopback_Fs)).real
    output *= 1.0 / np.percentile(np.abs(output), 95)
    output = np.r_[output, np.zeros(int(.1 * loopback_Fs))]
    if mask_noise is not None:
        if mask_noise.size > output.size:
            output = np.r_[output, np.zeros(mask_noise.size - output.size)]
        output[:mask_noise.size] += mask_noise[:output.size]
    delay_samples = int(delay * loopback_Fs)
    # delay one channel slightly relative to the other:
    # this breaks up the spatially-dependent frequency-correlated
    # nulls of our speaker array
    output = np.vstack((np.r_[np.zeros(delay_samples),
                              output], np.r_[output,
                                             np.zeros(delay_samples)])).T
    return output
Пример #11
0
def channelModel(output, lsnr):
    # received symbols
    input = np.copy(np.r_[output, np.zeros(32)])
    freq_offset = 232e3 * (2 * np.random.uniform() - 1)
    input *= np.exp(2 * np.pi * 1j * freq_offset * np.arange(input.size) /
                    20e6)
    phase_offset = 2 * np.pi * np.random.uniform()
    input *= np.exp(1j * phase_offset)
    # random causal 16-tap FIR filter with exponential tail
    h = (np.random.standard_normal(16) + 1j *
         np.random.standard_normal(16)) * np.exp(-np.arange(16) * 5.4 / 16)
    h[0] += 4.
    h /= np.sum(np.abs(h)**2)**.5
    input = np.convolve(input, h, 'same')
    input = util.upsample(input, 16)
    time_offset = np.random.random_integers(0, 15)
    input = input[time_offset::16]
    snr = 10.**(.1 * lsnr)
    input = add_noise(input, np.var(input) * 64. / 52. / snr)
    return input
Пример #12
0
def processOutput(output,
                  loopback_Fs,
                  loopback_Fc,
                  upsample_factor,
                  mask_noise,
                  percentile=95):
    output = util.upsample(output, upsample_factor)

    ramp_length = int(loopback_Fs * .01)
    output[:ramp_length] *= np.clip(
        np.arange(ramp_length, dtype=float) / ramp_length, 0, 1)
    output[-1:-ramp_length - 1:-1] *= np.clip(
        np.arange(ramp_length, dtype=float) / ramp_length, 0, 1)

    carrier = np.exp(1j * 2 * np.pi * np.arange(output.size) * loopback_Fc /
                     loopback_Fs)
    y1 = (output * carrier).real
    for i in xrange(13):
        y1 = np.diff(np.r_[0, y1])

    peak = np.percentile(np.abs(y1), percentile)
    gain = 16.0 / peak if peak else 0.
    output = gain * y1
    if 0:
        limit = 800.
        output = (2 / np.pi * limit) * np.arctan((np.pi / 2 / limit) * output)
    output = np.r_[output, np.zeros(int(.05 * loopback_Fs))]
    if mask_noise is not None:
        if mask_noise.size > output.size:
            output = np.r_[output, np.zeros(mask_noise.size - output.size)]
        output[:mask_noise.size] += mask_noise[:output.size]
    delay_samples = int(delay * loopback_Fs)
    # delay one channel slightly relative to the other:
    # this breaks up the spatially-dependent frequency-correlated
    # nulls of our speaker array
    output = np.vstack((np.r_[np.zeros(delay_samples),
                              output], np.r_[output,
                                             np.zeros(delay_samples)])).T
    global waveform
    waveform = output
    return output
Пример #13
0
def prepareMaskNoise(fn, Fs, Fc, upsample_factor):
    f = wave.open(fn)
    nframes = f.getnframes()
    dtype = [None, np.uint8, np.int16, None, np.int32][f.getsampwidth()]
    frames = np.fromstring(f.readframes(nframes), dtype).astype(float)
    frames = frames.reshape(nframes, f.getnchannels())
    frames = frames.mean(1)
    frames = util.upsample(frames, Fs / f.getframerate())
    frames /= np.amax(np.abs(frames))
    # band-stop filter for data
    frames *= np.exp(-1j * 2 * np.pi * np.arange(frames.size) * Fc / Fs)
    frames = iir.highpass(.8 / upsample_factor)(frames)
    frames *= np.exp(2j * 2 * np.pi * np.arange(frames.size) * Fc / Fs)
    frames = iir.highpass(.8 / upsample_factor)(frames)
    frames *= np.exp(-1j * 2 * np.pi * np.arange(frames.size) * Fc / Fs)
    frames = frames.real
    # look for beginning and end of noise
    envelope = iir.lowpass(.01)(np.r_[np.zeros(6), np.abs(frames)])[6:]
    start = np.where(envelope > np.amax(envelope) * .01)[0][0]
    end = np.where(envelope > np.amax(envelope) * 1e-3)[0][-1]
    return frames[start:end]
Пример #14
0
def prepareMaskNoise(fn, Fs, Fc, upsample_factor):
    f = wave.open(fn)
    nframes = f.getnframes()
    dtype = [None, np.uint8, np.int16, None, np.int32][f.getsampwidth()]
    frames = np.fromstring(f.readframes(nframes), dtype).astype(float)
    frames = frames.reshape(nframes, f.getnchannels())
    frames = frames.mean(1)
    frames = util.upsample(frames, Fs/f.getframerate())
    frames /= np.amax(np.abs(frames))
    # band-stop filter for data
    frames *= np.exp(-1j * 2 * np.pi * np.arange(frames.size) * Fc / Fs)
    frames = iir.highpass(.8/upsample_factor)(frames)
    frames *= np.exp(2j * 2 * np.pi * np.arange(frames.size) * Fc / Fs)
    frames = iir.highpass(.8/upsample_factor)(frames)
    frames *= np.exp(-1j * 2 * np.pi * np.arange(frames.size) * Fc / Fs)
    frames = frames.real
    # look for beginning and end of noise
    envelope = iir.lowpass(.01)(np.r_[np.zeros(6), np.abs(frames)])[6:]
    start = np.where(envelope > np.amax(envelope)*.01)[0][0]
    end = np.where(envelope > np.amax(envelope)*1e-3)[0][-1]
    return frames[start:end]
Пример #15
0
def main():
    opts = util.parse_args()
    X, y = util.data_load(opts.dataset)
    n = opts.upsamplen if opts.upsamplen is not None else 1
    start = n if opts.upsamplestart is None else 1
    if start > n:
        print("Upsample start should be larger than end")
        sys.exit()
    thresh = opts.threshold if opts.threshold is not None and opts.threshold >= 0.40 else None
    for t in np.arange(start, n + 1):
        needed = util.needed_n(X, y, t)
        temp_X, temp_y = util.upsample(X, y, needed)
        X_train, X_test, y_train, y_test = train_test_split(temp_X,
                                                            temp_y,
                                                            test_size=0.3,
                                                            random_state=42)
        X_train, X_test = util.normalize(X_train, X_test)
        clf = AdaBoostClassifier(n_estimators=100, random_state=0)
        clf.fit(X_train, y_train)
        conf_upsample = []
        if thresh is None:
            predictions = clf.predict(X_test)
            conf_mat = confusion_matrix(y_test, predictions)
            conf_upsample.append(conf_mat)
            print(conf_mat)
        else:
            conf_thresh = []
            for i in np.arange(0.4, thresh + 0.01, 0.005):
                predictions = (clf.predict_proba(X_test)[:, 1] >=
                               i).astype(int)
                conf_mat = confusion_matrix(y_test, predictions)
                conf_thresh.append(conf_mat)
                print(i)
                print(conf_mat)
            util.get_roc_curve(conf_thresh, "Adaboost", "threshold")
            plt.show()
Пример #16
0
     else:
         break
 if args[0] == '--rx':
     typeModHex = '--yubikey' in args
     doDiagnostics = ('--nodiags' not in args) and not typeModHex
     try:
         startListening()
     except KeyboardInterrupt:
         pass
     if doDiagnostics:
         decoderDiagnostics()
 elif args[0] == '--tx':
     startTransmitting()
 elif args[0] == '--wav-in' and len(args) > 1:
     input, Fs_file = util.readwave(args[1])
     input = util.upsample(input, Fs / float(Fs_file))
     input = audioLoopback.processInput(input, Fs, Fc, upsample_factor)
     for payload,_,_,lsnr_estimate in wifi.decode(input)[0]:
         print(repr(''.join(map(chr, payload))) + (' @ %.1f dB' % lsnr_estimate))
 elif args[0] == '--wav-out' and len(args) > 1:
     fn = args[1]
     args = args[2:]
     packets = 1
     while len(args):
         if args[0] == '--packets':
             packets = int(args[1])
             args = args[2:]
     outputChunks = []
     for i in xrange(packets):
         input_octets = ord('A') + np.random.random_integers(0,25,length)
         input_octets[:6] = map(ord, '%06d' % i)
Пример #17
0
     else:
         break
 if args[0] == '--rx':
     typeModHex = '--yubikey' in args
     doDiagnostics = ('--nodiags' not in args) and not typeModHex
     try:
         startListening()
     except KeyboardInterrupt:
         pass
     if doDiagnostics:
         decoderDiagnostics()
 elif args[0] == '--tx':
     startTransmitting()
 elif args[0] == '--wav-in' and len(args) > 1:
     input, Fs_file = util.readwave(args[1])
     input = util.upsample(input, Fs / float(Fs_file))
     input = audioLoopback.processInput(input, Fs, Fc, upsample_factor)
     for payload, _, _, lsnr_estimate in wifi.decode(input)[0]:
         print(
             repr(''.join(map(chr, payload))) +
             (' @ %.1f dB' % lsnr_estimate))
 elif args[0] == '--wav-out' and len(args) > 1:
     fn = args[1]
     args = args[2:]
     packets = 1
     while len(args):
         if args[0] == '--packets':
             packets = int(args[1])
             args = args[2:]
     outputChunks = []
     for i in xrange(packets):