randaller/cnn-rtlsdr

Install on Linux

bogdanr opened this issue · 33 comments

It would be great to have some instructions how to install this on Linux.

There are some DLLs mentioned in the instructions and also I think some stuff from requirements.txt might not apply to Linux.

There are no differences in installation process except of CUDA dlls, you may wish to install linux versions of cuda following https://www.tensorflow.org/versions/r1.2/install/install_linux under "NVIDIA requirements to run TensorFlow with GPU support" section. If you decide to use cpu version, you may skip this.

Not really all requirements.txt are required, i just did "pip freeze", including tons of my other installed libraries, then cleaned up some lines manually. All the rest should be in pip repo and easily installed on Linux or Mac with "pip install". If some lib still says "it's unavailable", you may try to skip it, may be it's not really needed. Only a few main key libraries are really needed, such as numpy/tensorflow/rtlsdr/keras, that's all exists for all of operating systems zoo.

I have Tensorflow-gpu and a complete set of typically used libraries on Ubuntu 16.04 which works for pretty much anything machine learning related that needs TF. It did not work on this occasion. I rebuilt a new virtual environment with the requirements-text list. I still does not work and throws the same error so two different set-ups throw the same error. I don't think it is the RTL-SDR in my case either it seems to work. Tensorflow does not like reading the 1200000 byte length and throws error code -8. So I just used the sample rate as is, that has now lead to new errors with the numpy array reshape function. I am just going to need to plod through, but the predict_scan.py is not working on Ubuntu as of today. I have a 12Gb GPU so its not out of memory. If anyone has any fresh insights that would be welcome. But I suspect some major rework on predict_scan.py is required at a guess to satisfy my particular case.

FYI the requirements-text: many of the file pointers are ahead in release numbers for linux so its asks for versions not yet released, just remove the version requirements where it breaks(all the ==x.x.x) and it will pull the latest available linux release. Some files are also windows only, just comment them out.

Nightmare on Elm Street! DO NOT install requirements.txt if you have Anaconda already installed!

As I was in a virtual environment after installing the requirements.txt, it wasn't apparent that it had 'smoked' Anaconda completely. The conda command refused to work and gave errors about being installed by pip. It was as I discovered irreversible. I have to destroy all my virtual environments and completely delete Anaconda owing to a bug with openssl so I could not write over the old install at all. I had a war with tensorflow-gpu and CUDA which I resolved eventually. I have now tensorflow-gpu running on 1.5.0 and CUDA running 9.0. Still it produced the same error. Apart from scipy and pyrtlsdr I think it didn't need much else to get it back to where I was previously. What a day!

I have tried to install it on Ubuntu 16.04 x64 and stuck with the same error -8 while reading from rtlsdr. Probably it should work somewhere, as spoken in pyrtlsdr bugtracker, but not with my dongle.

sudo su

apt-get install git
git clone https://github.com/randaller/cnn-rtlsdr.git
cd cnn-rtlsdr

apt-get install python3-pip
apt-get install rtl-sdr

pip3 install --upgrade pip

pip3 install tensorflow
pip3 install pyrtlsdr
pip3 install scipy

[remove dongle]
rmmod dvb_usb_rtl28xxu rtl2832
[insert dongle]

python3 predict_scan.py

Its not a dongle thing as far as I can see. I am going through to see where it breaks. Before this its failed after the arguments. The dongle itself is a one shot thing it must be closed and opened again from what I have seen slowing things down. It could be pyrtlsdr, its on the list. I will read the issues. Thanks for getting back at least we see the same issue thats a start :)

Did the same with latest Kali Linux, just with some modifications:

apt-get install git
git clone https://github.com/randaller/cnn-rtlsdr.git
cd cnn-rtlsdr

apt-get install python3-pip
apt-get install kali-linux-sdr

pip3 install tensorflow
pip3 install pyrtlsdr
pip3 install --upgrade numpy

python3 predict_scan.py

And got the same:
OSError: Error code -8 when reading 1200000 bytes

So I'm pretty sure it's pyrtlsdr/my dongle error, because gqrx/gnuradio works well on both Ubuntu and Kali.

ok. I have a HackRF unit as well but I only got the gear this week so I have had a steep learning curve ahead. I could try that unit but bending the code might take some time. I note the error we have is an 'OS' error not 'IO' error as reported in pyrtlsdr issues, at least that's what I read so far. I did an rtl_test it seems fine. I even got it to produce audio, but when it comes to using python and the RTL that's where the wheels are falling off using Linux.

Finally I figured it out. Rtl-sdr driver under Linux won't return read_bytes(value), if the value is not divides to powers of 2 (or multiple to read buffer size) or so. Rewriting read_samples() a bit - solves the problem and then it works well.

def read_samples(sdr, freq):
    sdr.center_freq = freq
    time.sleep(0.04)
    samples = sdr.read_samples(1221376)  # for example :)
    return samples[0:600000]

Image

I have noticed, that the predicted signal class at frequencies was shown as 'tv', not as correct 'wfm'. It is ok here :), due to folders structure. I am recommending not to use outdated first version, but prefer keras one. It should run also well, just do not forget:

pip3 install keras==2.0.1
pip3 install sklearn

Fabulous! I have mine working on the GPU at last. Thanks for the update. It had to be a data thing. I got nothing at first so I changed the antenna for something better. My high gain antenna will need moving, the number cruncher is too far from that, its raining, its probably not happening today. Running predict its absolutely convinced that every station it has received on the FM band is DMR which I interpret as Digital Microwave Radio, if thats what you mean, it will not be at ~100Mhz. I have some 'real' receivers I will take a listen as see what it has got mixed up on. But I suspect I will need to train this to local conditions, lets see how that goes. Keras is installed!

But it's still finding wfm stations on it's correct frequencies, right? :) Yes, it's better to train new model using your local antenna etc. Ok, here are some quick steps to run keras version now:

First, edit read_samples() function inside of [prepare_data.py] (to avoid driver errors under Linux): replace

iq_samples = sdr.read_samples(sample_rate * 0.25)  # sample 1/4 sec

with

iq_samples = sdr.read_samples(1221376)
iq_samples = iq_samples[0:600000]

Then scroll down [prepare_data.py] to find

collect_samples(942200000, "gsm") 

lines. Edit all of them, set frequencies and labels according to your local ether, save the file and then run it. It should collect some training samples. Sample .npy files should be 200,080 bytes each.

Before running [train_keras.py] you also need to edit a bit. Edit read_samples() function same way like we did with [prepare_data.py] :). Then scroll down file till the end, to find

check(422600000, "tetra")

lines. Also edit all of them - type there also frequencies and correct labels, just use DIFFERENT frequencies, that are not stored in [prepare_data.py] - trained model should be evaluated using new data from frequencies, that neural network never seen before.

I more or less did that I got it to create the .npy files, but I will do your method verbatim. I hit a snag. Given the time to preprocess, could tell me what the len(*.npy) should look like at the np.shape of *.npy. I had the training blow a gasket trying to reshape it to (128,2). It was saying it was a list of 54468 or something like that size not a numpy array during training. I took a look at the *.npy files they were complex number arrays and the iq_samples generated by preprocess were floats. But the *.npy were only half the size at around 25xxx bytes I don't have a baseline to compare fo what size and shape I should have but I know it will be 2D into keras. I will groom this again tomorrow with the above, we are out of sync in time zones, I suspect its on the second round now so I won't disrupt it. ....Cheers

I have been around in circles. I thought I had an SNR issue when I started training my models. It was all over the place. I have wripped all the training and testing files I did out and put yours back in. It seems there was only 1 *.npy file in each folder, I was creating dozens of them in my training for some reason. So now I am studying your original output again, to see what I can do about SNR or if its a FM format issue I think our RDS carrier is different to the U.S model in any case there must be minor differences or the prepare.py has placed the WFM files in the DMR folder on your end as it has located all the local FM stations but labeled them as DMR. There are stations on each freq I checked, so it seems the label is wrong. Still checking to see what is going on.

ok This is what I have before I forget:

  1. It does recognise FM stations its just labeled DMR. So that part works
  2. Train_keras will run but produces no checkpoint files.
  3. Train.py throws an error without the checkpoint files being in place.
  4. Dataset.py throws an Assertion error on line 94 when train.py is used.
    All I am doing is retraining your data, step by step. So I have changed nothing but the iq_samples, its all your data I am training on as per your files. So what I did not do is prepare data as I have data as provided. If thats wrong thats the issue, but as far as I can tell it should just train over what we have in place. It does not.

step-by-step

  1. Correct, and that's Ok. We do not keeping labels, we're reading them on-the-fly from the training_data/folder name. When the trained model labels count and folders count mismatch, it would produce shifted class name at predict_scan.py and/or tensorflow shape errors while running train.py.
  2. That's it. It trains network quickly in a few minutes, then opens rtl-sdr and tries to predict signal type at check() frequencies to validate model. No checkpoints being saved, it's easy to add last one save as
model.save(filename)

after fit().

  1. Yes, due to [1] reasons. Cleanup [training_data] folder completely. Collect samples using [prepare_data.py]. You need not dozens of samples per class, but hundreds or even thousands. Then delete 4 files in root folder: [checkpoint] and [rtlsdr-model*] before training start. Now you may run train or train_keras.

Dataset.py:94 error tells me, that you has a very little amount of training samples (less than 16). I repeat, you need a lot of, for example, 0.5-1 Gigabyte per each class, which is equal to 2000-4000 .npy files per class. Each npy file contains 12500 I and 12500 Q values interleaved, stored as 64-bit floats.

Also, neural network does not cares about RDS or anything that are modulated inside, as we didn't extracting such features, network just learns for "projected shape of cutted signal". And yes, SNR is very important, especially while working with signal from rtl-sdr dongles, I recommend to always set dongle gain to 'auto', before you completely understand of what's going on in the software.

ah right I see. It makes sense sure I have the model, but I assumed I had all the original *.npy too. So the missing ingredient is all the *.npy files. At no time has the GPU broken a sweat doing this stuff at all, super quick. Ok I will run preprocess tomorrow blow out the folders and check point. I just need to find some more interesting stuff the feed it. Cheers

Oh, that's the great deal of work. After the whole things ran successfully, shrink bandwidth and classify different FSK/BPSK`s. Classify multiple signals presence at time. Feed CW samples to LSTM network and decode Morse. Decode voice from samples. :) Decode patterns from stars. Automaticaly choose the best modulation mode while communicating digitally...

My 40m dipole is upstairs, that does well on WWV and HF but I am not lugging all of this upstairs. In any case I want to bend the code so I can do more QAM demod with the hackRF unit in the ISM bands. I could use the RSA306 but Tektronix never produced much info a driver of the IQ - too hard basket. So once I am comfortable with this RTL I will try and get the other working. I did voice using Wavenet I was feeding DTMF into it as a MNIST style test. Strange results not what I would have expected. But this is more predictable.

I modified here on train_keras

def read_samples(freq):
f_offset = 250000 # shifted tune to avoid DC
sdr.center_freq = freq - f_offset
time.sleep(0.06)
#iq_samples = sdr.read_samples(sample_rate * 0.25) # sample 1/4 sec
iq_samples = sdr.read_samples(1221376)
iq_samples = iq_samples[0:600000]
fc1 = np.exp(-1.0j * 2.0 * np.pi * f_offset / sample_rate * np.arange(len(iq_samples))) # shift down 250kHz
iq_samples = iq_samples * fc1
return iq_samples

and here only

check(88700000, "wfm")
check(492500000, "tv")
check(89600000, "wfm")
check(104900000, "wfm")
check(1080000000, "tetra")
check(100500000, "wfm")
check(120000000, "other")
check(592500000, "tv")
check(827500000, "3G")
check(662500000, "tv")

and it just tests everything as 'tetra'

88.7 tetra 99.95344877243042
492.5 tetra 99.93725419044495
89.6 tetra 99.94871616363525
104.9 tetra 99.9605119228363
1080.0 tetra 99.7898817062378
100.5 tetra 99.94792342185974
120.0 tetra 99.95473027229309
592.5 tetra 99.92196559906006
827.5 tetra 99.89325404167175
662.5 tetra 99.91363883018494

still no check point files generated. I must have missed something. I changed prepare it created all the *.npy files ok and train_keras trained ok for 50 iterations it looked fine. Not sure what I missed as it not throwing errors.

I am not sure what the role of data2 does btw there maybe an issue in there, it references iq_samples but nothing from the RTL_SDR specifically i could see.

This is from prepare_data line 44 +

collect_samples(940000000, "tetra")
collect_samples(92700000, "wfm")
collect_samples(872500000, "3G")
collect_samples(606500000, "tv")

These are the directories in training _data
3G other tetra tv wfm

and testing_data
3G other tetra tv wfm

there's 750 files in each directory e.g. ~/cnn-rtlsdr/training_data/wfm

Now clearly thats not 2000 thats all it created so I must need to change collect_samples?

Testing_data folder is not really needed for keras version, as it takes 30% from training_data to use as testing_data. What are keras latest statistic values after training? loss, acc, val_loss and val_acc? Did you set ppm value correctly?

Sharing my training data, that should give: loss: 0.01, acc: 0.9971, val_loss: 0.0193 and val_acc: 0.9961. Just did a fresh train (on windows) and it has correctly predicted fresh signals from ether.
https://drive.google.com/open?id=1PuhzXkk6AVwXPPKjtFUCpQVsqOOlszu8

My tv carrier is SECAM so it would differ, but network should predict tetra & wfm correctly on your ether.

.....and the answer is...h5py! A casualty of requirements.txt disaster where I had to rebuild the system from scratch thus losing many libraries I had built in over time. So where does that leave us?

  1. train.py now works. Created checkpoints which satisfy predict_scan.
  2. train_keras.py works and even makes half an attempt at the correct answers.
  3. predict_scan says of checkpoint model that everything is now tetra much the same as train_keras.py did yesterday.
  4. I need to somehow merge in the h5py into predict_scan code, i solved one issue but created another seems it wants labels?.

So its a mystery during training that keras_train come up with half decent answers but train.py said its all tetra. But I can't run predict on the h5py model as is without changes to truly run keras out on its own.

I removed compile and fit and run it, its a bit all over the place with predictions. SECAM is an old analog european standard we have digital TVB, also gsm is gone here I think, its wideband 3g or 4g so I am sampling multi carrier or cdma wideband QAM systems not analog. So I will hit it with more samples today which takes hours to do. I am not sure I have the gain right. I have a preamp on the rtf-sdr it may be causing intermod on some signals, hard to say with digital sounds ok on fm. So if that fails to predict accurately I will just concentrate on getting fm right as a baseline, then attempt to adjust for QAM based signals. The decimation maybe to harsh with signals spanning more than 5 Mhz, I may need more sample bandwidth.

Hi
I did the same thing that you do and didn't get any output from my dongle. I used UBUNTU 16.04 and install Anaconda. after that I installed the Requirement.txt packages and RTL-SDR driver. after that I changed the read_samples function and run the predict_scan.py but as you see nothing happened and get none in result.
What's can be wrong ? Can somebody please help me?
@randaller
screenshot from 2018-02-12 07-07-08

Those are just info and deprecation warnings. It may not have detected a signal at all.

First I would do a sanity check as follows on the command line:

rtl_fm -f 92.7e6 -M wbfm -s 240000 -r 48000 - | aplay -r 48k -f S16_LE

Change 92.7e6 for a local station you have. Do you hear sound? If so proceed next. I can see it has PLL warnings so it should be ok. If not sound card issues not our problem with raw samples but you should get the radio working its essential. Check you have 'aplay' check 'stack overflow' for more details.

I would insure you have h5py installed. Use 'pip freeze' on the cmd line, look for h5py==2.7.0.

Are you using Python 3.6? I can't speak for 2.7. can't say it will or won't work but given the number of print statements in code you would hit some errors very quickly. Did you install Tensorflow for Python 3+, I see its cpu only no issue with that, certainly easier. You could be running Py 3.6 on the wrong tensorflow. Fire-up python interpreter and issue 'import tensorflow'. Did that work? If not, sort that out.

I would did a 'pip freeze' check against requirements.txt. Also you may want to use 'conda list' and see what anaconda installed.

That should keep you busy for a while. Also I have a weak signal I would take the RTL stick off 'auto' in the prepare_data.py code and replace with 60 which as far as I can tell is max gain (line 42 in my code).

Try that!

I see python 3.6 being used, signal.decimate() tells this. After the check, ensuring that rtl-sdr works well with other applications, and if cnn-rtlsdr still ends silently, try one of my ways to install environment on a clean Ubuntu or Kali, edit the function, and check again, it really should detect at least wfm at it's places. Otherwise, I don't know more what could help, may be inserting debug print( ) after each block of code to see what's happening line by line.

So I have followed all the steps, when I run Python_predicts this is where I am stuck..

~/cnn-rtlsdr$ sudo python3 predict_scan.py --start 850000000 --stop 860000000 --threshold 0.9955
Detached kernel driver
Found Rafael Micro R820T tuner
[R82XX] PLL not locked!
2018-11-15 07:54:51.714785: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
/usr/local/lib/python3.6/dist-packages/scipy/signal/_arraytools.py:45: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use arr[tuple(seq)] instead of arr[seq]. In the future this will be interpreted as an array index, arr[np.array(seq)], which will result either in an error or a different result.
b = a[a_slice]
/usr/local/lib/python3.6/dist-packages/scipy/signal/signaltools.py:3463: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use arr[tuple(seq)] instead of arr[seq]. In the future this will be interpreted as an array index, arr[np.array(seq)], which will result either in an error or a different result.
return y[sl]
Reattached kernel driver

the sdr works as I have ran it through GQRX but I cant seem to get past this..any help please?
any help would be appreciated also is the instructions for ubuntu all the commands that are needed?

It has just found nothing in ether. Commercial FM is usually at 88-108 MHz. Try with --start 88000000 --stop 108000000

It is much better to use fresh keras version of neural network.

Awesome, thank you for your help. So with Keras will it be able to reconize dmr. Also when I run it I get this error.

etached kernel driver
Found Rafael Micro R820T tuner
[R82XX] PLL not locked!
Traceback (most recent call last):
File "train_keras.py", line 112, in
check(92900000, "wfm")
File "train_keras.py", line 76, in check
iq_samples = read_samples(freq)
TypeError: read_samples() missing 1 required positional argument: 'freq'
Reattached kernel driver

thank you for your help

Commands for fresh installed Ubuntu 18.04.1 LTS, desktop version:

sudo su

apt install git
git clone https://github.com/randaller/cnn-rtlsdr.git
cd cnn-rtlsdr

apt install python3-pip
apt install rtl-sdr

pip3 install --upgrade pip

pip install tensorflow==1.5
pip install pyrtlsdr
pip install scipy

[remove dongle]
rmmod dvb_usb_rtl28xxu rtl2832
[insert dongle]

python3 predict_scan.py

Detached kernel driver
Found Rafael Micro R820T tuner
[R82XX] PLL not locked!
2018-11-26 15:26:04.158066: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX
/usr/local/lib/python3.6/dist-packages/scipy/signal/_arraytools.py:45: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use arr[tuple(seq)] instead of arr[seq]. In the future this will be interpreted as an array index, arr[np.array(seq)], which will result either in an error or a different result.
b = a[a_slice]
/usr/local/lib/python3.6/dist-packages/scipy/signal/signaltools.py:3463: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use arr[tuple(seq)] instead of arr[seq]. In the future this will be interpreted as an array index, arr[np.array(seq)], which will result either in an error or a different result.
return y[sl]
87.500 MHz - dmr 99.99%
88.000 MHz - dmr 100.00%
88.900 MHz - dmr 100.00%
89.700 MHz - dmr 100.00%
90.100 MHz - dmr 100.00%
90.500 MHz - dmr 96.31%
90.600 MHz - dmr 100.00%
91.100 MHz - dmr 99.88%
91.500 MHz - dmr 99.99%
92.900 MHz - dmr 99.61%
95.000 MHz - dmr 100.00%

Which is pretty ok, just showing dmr instead of correct wfm label due to training_data/ folder contents.

And to continue to keras version:

pip install keras
pip install sklearn

rm -r training_data

To use developer data:

or, to use own data

  • Edit prepare_data.py according to your frequencies and folders structure and run:
mkdir training_data
python3 prepare_data.py

Now we are ready to run keras version:

python3 train_keras.py

I followed the instructions but it doesn't work, getting Illegal instruction. please help. Thanks
I have latest version of kali linux
root@kali:~/cnn-rtlsdr/cnn-rtlsdr# python3 predict_scan.py
Illegal instruction

Hi,
having had so many errors in windows and even more errors in kali linux I just installed ubuntu to make a fresh start but still having problems

root@udd-K73E:/home/udd/cnn-rtlsdr# python3 predict_scan.py
/usr/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: compiletime version 3.5 of module 'tensorflow.python.framework.fast_tensor_util' does not match runtime version 3.6
return f(*args, **kwds)
Detached kernel driver
Found Rafael Micro R820T tuner
[R82XX] PLL not locked!
2019-03-05 18:02:32.018359: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2

I googled the issue and found that I should install an older version so I installed tensorflow1.4 but still getting errors...

root@udd-K73E:/home/udd/cnn-rtlsdr# python3 predict_scan.py
/usr/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: compiletime version 3.5 of module 'tensorflow.python.framework.fast_tensor_util' does not match runtime version 3.6
return f(*args, **kwds)
Detached kernel driver
Found Rafael Micro R820T tuner
[R82XX] PLL not locked!
2019-03-05 17:22:15.758252: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2
^CTraceback (most recent call last):
File "predict_scan.py", line 59, in
iq_samples = read_samples(sdr, freq)
File "predict_scan.py", line 14, in read_samples
iq_samples = sdr.read_samples(1221376)
File "/usr/local/lib/python3.6/dist-packages/rtlsdr/rtlsdr.py", line 497, in read_samples
iq = self.packed_bytes_to_iq(raw_data)
File "/usr/local/lib/python3.6/dist-packages/rtlsdr/rtlsdr.py", line 514, in packed_bytes_to_iq
iq /= 127.5
KeyboardInterrupt
Reattached kernel driver

would appreciate some help. thanks newbee

Hi

What remains to be sorted out for it to work under Ubuntu 16.04/18.04?